Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outer Join The Rails 3 Way

i have 3 models like :

location, user, discovered_location(location_id,user_id)

I think i need an outer join in order to get all locations, as well as include the discovered_location model, if that location has been discovered by the user.

I would need something like {location1, location2, location3:includes discovered_location, location4 ..}

Is there a Rails 3 way to do that ? If not, what is the best way ?

EDIT

I want to get the locations specified above, for a certain user. To better illustrate, it should be :

user {location1, location2, location3:includes discovered_location, location4 ..}

(A user has many discovered locations)

like image 858
Spyros Avatar asked Dec 06 '22 22:12

Spyros


1 Answers

You can do an outer join in Rails only by using an SQL literal in the joins method:

joins("OUTER JOIN table2 on table2.column = table1.column")
like image 170
Ryan Bigg Avatar answered Feb 27 '23 14:02

Ryan Bigg