This project is archived and is in readonly mode.
.count on a named_scope on a has_many association throws error
Reported by Cameron Booth | April 16th, 2009 @ 05:06 AM | in 2.x
I'm hitting a bug working on an upgrade from Rails 2.2 to 2.3. It occurs in the following case:
class User < ActiveRecord::Base
has_many :memberships
has_many :challenges, :through => :memberships
end
class Membership < ActiveRecord::Base
belongs_to :challenge
belongs_to :user
end
class Challenge < ActiveRecord::Base
named_scope :blue, :conditions => {:name => 'blue' }
end
# "blue" is just for this test case, made up
The error occurs when calling:
some_user.challenges.blue.count
It tries to generate invalid SQL like this:
SELECT count("challenges".*) AS count_challenges_all FROM "challenges" INNER JOIN "memberships" ON "challenges".id = "memberships".challenge_id WHERE ((("challenges"."name" = 'blue') AND (("memberships".user_id = 1))) AND (("memberships".user_id = 1)))
The problem as far as I can tell is the "SELECT count("challenges".) part. It works if its is "SELECT count()" or "SELECT count("challenges".id)
(Small note, it also adds the ("memberships".user_id = 1) condition two times, but that's not invalid, it's just excessive, and new in 2.3)
I will start to dive into the source code to see if I can narrow this down a bit.
Comments and changes to this ticket
-
Cameron Booth April 16th, 2009 @ 05:20 AM
I used git bisect to narrow the cause down to this commit:
http://github.com/rails/rails/co...
I'll look now at creating a test case, and possibly fixing it if I can figure it out
-
Max Lapshin April 23rd, 2009 @ 10:55 AM
- Tag changed from 2.3.2, :through, active_record, sql to 2.3.2, :through, active_record, duplicate, sql
-
Matt Jones May 10th, 2009 @ 11:28 PM
- State changed from new to duplicate
As noted, duplicate of #2189.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>