This project is archived and is in readonly mode.

#2507 ✓duplicate
Cameron Booth

.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

class Membership < ActiveRecord::Base
  belongs_to :challenge
  belongs_to :user

class Challenge < ActiveRecord::Base
  named_scope :blue, :conditions => {:name => 'blue' }
# "blue" is just for this test case, made up

The error occurs when calling:

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

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=""></a>

People watching this ticket