Alex Rivera | Logout

LEFT OUTER joins in Rails 3

Asked 2010-07-14T10:22:29.507
87

I have the following code:

@posts = Post.joins(:user).joins(:blog).select

which is meant to find all posts and return them and the associated users and blogs. However, users are optional which means that the INNER JOIN that :joins generates is not returning lots of records.

How do I use this to generate a LEFT OUTER JOIN instead?

Edit
Report

1 Answer

114
@posts = Post.joins("LEFT OUTER JOIN users ON users.id = posts.user_id").
              joins(:blog).select
answered 2010-07-14T12:31:02.033

Your Answer