'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



Solution 1:[1]

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..

Solution 2:[2]

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])

Solution 3:[3]

you can use the method left_joins:

@recent_details = Property.left_joins(:space_amenities)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thorin
Solution 2 Oskars Celmalnieks
Solution 3 Motine