Here's the simplest way I can think of to describe the problem.
Using the classic posts, authors and comments I need to query for an authors posts and any comments (there may be zero to many comments) for a specific author. Database is postgressql 9.2 and Rails 4.0.
The query should come out something like (this isn't valid sql, need to show that author_id needs to be dynamic):
SELECT *
FROM POSTS
JOIN AUTHORS ON posts.author_id = authors.id
LEFT OUTER JOIN comments ON comments.posts_id = posts.id AND comments.author_id = #{author_id}
WHERE posts.author_id = #{author_id}
The second condition on the join is the secret sauce here that gives the right results and allows this to be eager loaded (not to mention extremely fast). The author_id in that second condition should be dynamic too.
In postgresql rewriting this query to move the second join condition to the WHERE clause does not produce correct results. (In this case, the post has to have comments from the author_id for it to be included in the results.)
How do I get the second join condition in active record? Resorting to a n+1 situation is not an option for performance this needs to be eager loaded or at worst done with two queries.
If this ever gets picked up by a search engine, I've included some working code that shows how to deal with this. Notice the lambda on the has_many :author_comments. Thanks to @erniemiller.