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. -- Posted via http://www.ruby-forum.com/.
You have to use .size not .total On Mon, Jul 6, 2009 at 6:19 PM, Rails List <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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. > -- > 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 -~----------~----~----~----~------~----~------~--~---
Sergio Angeletti wrote:> You have to use .size > > not .total > > On Mon, Jul 6, 2009 at 6:19 PM, Rails List > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.orgSize 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(*)" -- Posted via http://www.ruby-forum.com/.
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 List <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Sergio Angeletti wrote: > > You have to use .size > > > not .total > > > On Mon, Jul 6, 2009 at 6:19 PM, Rails List > > <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > > 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(*)" > -- > Posted viahttp://www.ruby-forum.com/.
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 List <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Sergio Angeletti wrote: > > You have to use .size > > > not .total > > > On Mon, Jul 6, 2009 at 6:19 PM, Rails List > > <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > > 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(*)" > -- > Posted viahttp://www.ruby-forum.com/.
s/along/alone On Jul 7, 12:43 am, Arzumy <he...-/do3P1lf25I@public.gmane.org> wrote:> 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 List <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Sergio Angeletti wrote: > > > You have to use .size > > > > not .total > > > > On Mon, Jul 6, 2009 at 6:19 PM, Rails List > > > <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > > > 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(*)" > > -- > > Posted viahttp://www.ruby-forum.com/.