Forum: Ruby on Rails Select count(*) using Ruby on Rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2119f016588ba13373484491bd2dd6d1?d=identicon&s=25 Joe Peck (fatcatt316)
on 2008-12-11 17:05
Hullo.  Welcome to my question.

In my controller I need to get the number of items in a table.

If I was just using MySQL, I'd do "select count(*) from blah where blah
blah blah".

What is the way to do this in my controller?  I'd like to avoid
something like Blah.find(:all, :conditions => blah blah).size

Is there a way to do the equivalent of select count(*) in the
controller?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-11 17:11
(Received via mailing list)
On 11 Dec 2008, at 16:05, Joe Peck wrote:

> something like Blah.find(:all, :conditions => blah blah).size
>
Blah.count(:all, :conditions => ...)

or if you had a named_scope for that set of conditions,
Blah.some_scope.count

Fred
2119f016588ba13373484491bd2dd6d1?d=identicon&s=25 Joe Peck (fatcatt316)
on 2008-12-11 17:15
Frederick Cheung wrote:
> On 11 Dec 2008, at 16:05, Joe Peck wrote:
>
>> something like Blah.find(:all, :conditions => blah blah).size
>>
> Blah.count(:all, :conditions => ...)
>
> or if you had a named_scope for that set of conditions,
> Blah.some_scope.count
>
> Fred

Beautiful.  Thanks Fred, you seem to answer a lot of questions on this
board.
This topic is locked and can not be replied to.