lp.dahito-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jul-05 03:11 UTC
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Fri, Jul 4, 2008 at 11:11 PM, lp.dahito-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <lp.dahito-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Craig Demyanovich wrote:> On Fri, Jul 4, 2008 at 11:11 PM, lp.dahito-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > <lp.dahito-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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, > CraigOpinion.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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sat, Jul 5, 2008 at 3:28 PM, Wu Junchen <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Craig Demyanovich wrote: >>> Opinion.count( :conditions => "agree == true" ) >> Opinion.count(:conditions => "agree = true")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 -- http://lindsaar.net/ Rails, RSpec, Puppet and Life blog.... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
lp.dahito-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jul-05 14:05 UTC
Re: How do you count Booleans in a model ???
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sat, Jul 5, 2008 at 2:22 AM, Mikel Lindsaar <raasdnil-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Sat, Jul 5, 2008 at 3:28 PM, Wu Junchen > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> Craig Demyanovich wrote: >>>> Opinion.count( :conditions => "agree == true" ) >>> Opinion.count(:conditions => "agree = true") > > 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])Excellent point, Mikel. Thanks, Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---