Hello all, Can someone help me with this query ? Here is the query used in rails 2. The idea here is to obtain the top 10 widgets. Widget.find(:all, :select => ''widget_type_id, COUNT(widget_type_id) AS widgettypeid'', :group => ''widget_type_id'', :order => ''widgettypeid DESC'', :limit => 10) This is deprecated and i want to port it to rails 3. Thanks for your help. Dominique. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tony Primerano
2010-Sep-08 13:42 UTC
Re: Rails 2 to Rails 3 - How to convert this sql query ?
I''m not sure how to do the "COUNT(widget_type_id) AS widgettypeid" as this doesn''t seem to work below but it is close. hoping someone will chime in. :-) I''m sure I''m missing something simple. Widget.group(:widget_type_id).select(''widget_type_id, count(widget_type_id) AS widgettypeid'').order(:widgettypeid).limit(10) No docs on this at http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-group but guide has some examples. http://guides.rubyonrails.org/active_record_querying.html On Sep 8, 5:35 am, dme69 <dmeg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello all, > > Can someone help me with this query ? > > Here is the query used in rails 2. The idea here is to obtain the top > 10 widgets. > Widget.find(:all, :select => ''widget_type_id, COUNT(widget_type_id) AS > widgettypeid'', :group => ''widget_type_id'', :order => ''widgettypeid > DESC'', :limit => 10) > > This is deprecated and i want to port it to rails 3. > > Thanks for your help. > > Dominique.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Great ! This seems to work perfectly for me. Thanks for your help. I will take a big look at this to understand how it is working. Dominique. On 8 sep, 15:42, Tony Primerano <tony.primer...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m not sure how to do the "COUNT(widget_type_id) AS widgettypeid" as > this doesn''t seem to work below but it is close. hoping someone will > chime in. :-) I''m sure I''m missing something simple. > > Widget.group(:widget_type_id).select(''widget_type_id, > count(widget_type_id) AS widgettypeid'').order(:widgettypeid).limit(10) > > No docs on this athttp://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#met... > but guide has some examples. http://guides.rubyonrails.org/active_record_querying.html > > On Sep 8, 5:35 am, dme69 <dmeg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hello all, > > > Can someone help me with this query ? > > > Here is the query used in rails 2. The idea here is to obtain the top > > 10 widgets. > > Widget.find(:all, :select => ''widget_type_id, COUNT(widget_type_id) AS > > widgettypeid'', :group => ''widget_type_id'', :order => ''widgettypeid > > DESC'', :limit => 10) > > > This is deprecated and i want to port it to rails 3. > > > Thanks for your help. > > > Dominique.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.