Forum: Ruby on Rails Extending left outer joins when using active eager loading

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
3cbe6157fca13c0fbbb5ed81b803c555?d=identicon&s=25 JW (Guest)
on 2007-06-16 00:15

From the ActiveRecord::Associations::ClassMethods docs, there is the

"for post in Post.find(:all, :include => [ :author, :comments ])"

"That‘ll add another join along the lines of: LEFT OUTER JOIN comments
ON comments.post_id ="

What can one do to add a DYNAMIC second AND constraint to the left outer
join on clause such as:

"LEFT OUTER JOIN comments ON comments.post_id = AND
comment.person_id = ?"

I can only do this via a custom association where the comment.person_id
value is hard-coded, but it's of very limited use.  I want
comment.person_id to reflect the logged in user or a session id, etc.
(Perhaps a limitation of AR?)

I've always needed to go to find_by_sql for this and would LOVE to stay
with ActiveRecord, instead.

Many thanks,

A887cc76c61e1f4148caacb5f32361d6?d=identicon&s=25 jparker (Guest)
on 2007-06-16 19:19
(Received via mailing list)
If the goal is to find a Post (or all Posts) including associated
Comments by a particular Person, you could stick with #find if you
moved the Comment-Person constraint to the WHERE clause, i.e.,

  :include => [:author, :comments],
  :conditions => ['comments.person_id = ?', x]

3cbe6157fca13c0fbbb5ed81b803c555?d=identicon&s=25 JW (Guest)
on 2007-06-20 00:05

Thanks for your suggestion and time, John.  It wasn't exactl what we
were after, but luckily we did find it here:

Hope this helps someone else as well.

This topic is locked and can not be replied to.