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.
Joe P. (Guest)
on 2008-12-11 18: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?
Frederick C. (Guest)
on 2008-12-11 18:11
(Received via mailing list)
On 11 Dec 2008, at 16:05, Joe P. 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
Joe P. (Guest)
on 2008-12-11 18:15
Frederick C. wrote:
> On 11 Dec 2008, at 16:05, Joe P. 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.