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? -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Jon Leighton
2011-Jan-11 17:36 UTC
Re: Should includes with general association name work?
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 On Tue, 2011-01-11 at 12:05 -0500, Rick DeNatale wrote:> 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? > > > -- > Rick DeNatale > > Blog: http://talklikeaduck.denhaven2.com/ > Github: http://github.com/rubyredrick > Twitter: @RickDeNatale > WWR: http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn: http://www.linkedin.com/in/rickdenatale >-- http://jonathanleighton.com/