rails: include statement with two ON conditions

Posted by Markus on Stack Overflow See other posts from Stack Overflow or by Markus
Published on 2010-05-03T16:02:38Z Indexed on 2010/05/03 16:28 UTC
Read the original article Hit count: 396

Filed under:
|
|

Hi,

I have tree tables

  • books
  • bookmarks
  • users

where there is a n to m relation from books to users trough bookmarks.

Im looking for a query, where I get all the books of a certain user including the bookmarks. If no bookmarks are there, there should be a null included...

my sql statement looks like:

SELECT * FROM `books` 
LEFT OUTER JOIN `bookmarks ` 
ON bookmarks.book_id = books.id 
AND bookmarks.user_id = ?

In rails I only know the :include statement, but how can I add the second bookmarks.user_id = ? statement in the ON section of this query? if I put it in the :conditions part, no null results would get returned!

Thanks! Markus

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about sql