# Unable to do a count on named\_scope

**URL:** <https://www.ruby-forum.com/t/unable-to-do-a-count-on-named-scope/176053>\
**Category:** Rails\
**Created:** [September 24, 2009, 5:01am UTC](https://www.ruby-forum.com/t/unable-to-do-a-count-on-named-scope/176053 "2009-09-24T05:01:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [September 24, 2009, 5:01am UTC](https://www.ruby-forum.com/t/unable-to-do-a-count-on-named-scope/176053/1 "2009-09-24T05:01:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [September 24, 2009, 1:50pm UTC](https://www.ruby-forum.com/t/unable-to-do-a-count-on-named-scope/176053/2 "2009-09-24T13:50:10Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid
