How do you count Booleans in a model ???

Hey...

I'd like to knw how to count the number of trues or falses a boolean
in my model contains...

Example:

create_table :opinions do |t|
      t.boolean :agree
end

In this migration... if :agree is true... that means that the user,
for instance, agrees and if :agree is false, the user disagrees...

I'm trying o calculate is as follows....

o = Opinion.new
o.agree = true
o.save

And then... When I do this...

Opinion.count

it returns 1 wich is good...

But as soon as I want to know the number of Trues or Falses... doesnt
work !!!!!

Example:

Opinion.count( :conditions => "agree == true" )

please somebody help me out with this....

Is there a way of counting the number of boolean values in your
db ???

Thx

Louis-Pierre

Try "=" instead of "==".

Opinion.count(:conditions => "agree = true")

Something like that worked for me with one of my models in script/console.

Regards,
Craig

Craig Demyanovich wrote:

Example:

Opinion.count( :conditions => "agree == true" )

please somebody help me out with this....

Is there a way of counting the number of boolean values in your
db ???

Try "=" instead of "==".

Opinion.count(:conditions => "agree = true")

Something like that worked for me with one of my models in
script/console.

Regards,
Craig

Opinion.count(:conditions => "agree = true") will work fine.
I has thought Opinion.sum(:agree) will work fine too,but it didn't,this
generate the sql:
select sum('opinions'.agree) as sum_agree from 'opinions',
and returns 0 forever,I copy this sql and execute it by hand,it will
work fine just like the Opinion.count(:conditions => "agree = true").So
it maybe some strange?

This is a database specific solution and would break on PostgreSQL for example.

A better way unless you really know what you are doing is:

Opinion.count(:conditions => {:agree => true})

Which will work on any database.

or

Opinion.count(:conditions => ["agree = ?", true])

Mikel

Thx a lot guys !!!

Craig... I had already tried this before i came here to ask for
help... Thx anyway !

Mikel, you saved me... That's exactly what I needed.. thx a lot !!!

Louis-Pierre

Excellent point, Mikel.

Thanks,
Craig