Retrieving Count (*)

I have a find statement in my controller like the following:

@c = Model.find(:all, :select=>“count(*) as total”, :conditions=>[some
conditions])

This returns some value or “0” (if none found).

How do I retrieve this returned value?. I tried something like

@c.total but it says unknown attribute.

You have to use .size

not .total

On Mon, Jul 6, 2009 at 6:19 PM, Rails L.
<[email protected]

Sergio A. wrote:

You have to use .size

not .total

On Mon, Jul 6, 2009 at 6:19 PM, Rails L.
<[email protected]

Size doesn’t tell you the returned value. it just tells you the array
length/size. What I need is the mysql returned value. In this case it
would be the value of “count(*)”

Use the ActiveRecord::Calculations::Class Methods

i.e. Model.count(:conditions => “…”)

(look it up on api.rubyonrails.org for more details)

On Jul 6, 9:29 am, Rails L. [email protected]

Your @c is an array, so @c.first.total should work

Although, if you just want the size along, Model.count would be the
best way to go as per E. Litwin suggestion.

On Jul 7, 12:29 am, Rails L. [email protected]

Thanks a lot for all the replies. I’ll go with Model.count

I just realised “@c[0].total” also works.

s/along/alone