Should includes with general association name work?

Hi, I'm wondering if this is an arel bug or should be a new feature.

I've got a model with the following:

     has_many :recommendations, :class_name => "Referral", :foreign_key => :candidate_id     scope :recommended_to, lambda {|er| joins(:recommendations).where(:referrals => {:employer_representative => er})}

This works and generates the right inner join.

puts User.recommended_to(er).to_sql users" INNER JOIN "referrals" ON "referrals"."candidate_id" = "users"."id" WHERE ("referrals"."employer_representative" = '6')

But if I change joins to includes in the scope, I get: puts User.recommended_to(er).to_sql SELECT "users".* FROM "users" WHERE ("referrals"."employer_representative" = '6')

Which won't work since it needs the join.

I know that the old :include option only generated a join clause if there was also a condition which required it, and since the includes method doesn't know that it kind of makes sense. I also tried chaining the includes after the where in hopes that then include WOULD know, but it didn't make a difference.

Is there, or should there, be a way in such a case to force includes to generate the join as well as instantiating the associated records?

This looks like a bug to me. You should try a few things:

* Can you repro on master? * What about doing where('referrals.employer_representative' => ...) - does that make a difference? * Try doing the query manually (i.e. Users.includes(..).where(...)) - I doubt the scope is the problem but good to rule things out.

(BTW the bug is probably in ActiveRecord::Relation, not arel. Also, specifying the query in a different order won't make a difference since the query will only be built when it's needed, not incrementally.)

Cheers