Unable to do a count on named_scope

Hi all,

I’m not sure if this is a bug, but I’m having some problem doing a
count, on
a named_scope.

class Claim < ActiveRecord::Base
has_many :items
belongs_to :event
before_save :add_pv
accepts_nested_attributes_for :items, :allow_destroy => true

named_scope :pending, :conditions => {:status => “Pending”}
named_scope :approved, :conditions => {:status => “Approved”}
named_scope :endorsed, :conditions => {:status => “Endorsed”}

end

@user.club.claims.pending.count
ActiveRecord::StatementInvalid: Mysql::Error: You have an error in
your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near ') AS
count_claims_all FROM claims INNER JOIN events ON ( claims.event_id
= ev’ at line 1: SELECT count(claims.
) AS count_claims_all FROM
claims INNER JOIN events ON ( claims.event_id = events.id ) WHERE
(((claims.status = ‘Pending’) AND (events.club_id = 1 )) AND
(events.club_id = 1 ))

I don’t really know why count isn’t working here. Can someone advice?

Warmest regards,
David C.

David C. wrote:

Hi all,

I’m not sure if this is a bug, but I’m having some problem doing a
count, on
a named_scope.
[…]

@user.club.claims.pending.count
ActiveRecord::StatementInvalid: Mysql::Error: You have an error in
your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near ') AS
count_claims_all FROM claims INNER JOIN events ON ( claims.event_id
= ev’ at line 1: SELECT count(claims.
) AS count_claims_all FROM
claims INNER JOIN events ON ( claims.event_id = events.id ) WHERE
(((claims.status = ‘Pending’) AND (events.club_id = 1 )) AND
(events.club_id = 1 ))

I don’t really know why count isn’t working here. Can someone advice?

My guess – and it is a guess – is that mySQL is complaining about
the count(claims.) construct. You can have count(table.field) or
count(
), but I doubt that count(table.*) is valid. If I’m right, then
this is a bug in the SQL generation.

Warmest regards,
David C.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]