SkRoR
SkRoR

Reputation: 1199

How to do outer join in rails

I need to outer join in rails

@recent_details = Property.joins(:space_amenities,:event_suitabilities,:purpose_suitabilities,:venue_categories).where(id: params[:id])

this active record gives me inner join. but i need outer join with this active record.

please help Any help is appreciable

Upvotes: 3

Views: 13961

Answers (3)

Motine
Motine

Reputation: 1862

you can use the method left_joins:

@recent_details = Property.left_joins(:space_amenities)

Upvotes: 2

OskarsCC
OskarsCC

Reputation: 123

LEFT, RIGHT (OUTER) JOIN has all the same convention, when compiling JOINS, so basically You specify Your need:

@recent_details = Property.joins("LEFT OUTER JOIN space_amenities ON space_amenities.property_id = properties.id").where(id: params[:id])

Upvotes: 3

Thorin
Thorin

Reputation: 2034

you can use includes if you want LEFT OUTER joins or you can add manual joins like:

@recent_details = Property.joins("LEFT OUTER JOIN space_amenities ON space_amenities.property_id = properties.id ").where(id: params[:id])

By this types of joins you can add any types of the joins like RIGHT outer etc..

Upvotes: 2

Related Questions