I have this @temp = Articles.find(:all) @temp.sort_by { | articles | articles.rating }.reverse i just basically want my top 10 articles, do i need to plug in :limit => 10 someplace? thanks! -- 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 -~----------~----~----~----~------~----~------~--~---
Jason Norris
2006-Dec-11 16:29 UTC
Re: just want my first ten parts of the hash? how to chop it?
Articles.find(:all, :order => ''publication_date DESC'', :limit => 10 ) koloa wrote:> I have this > > @temp = Articles.find(:all) > @temp.sort_by { | articles | articles.rating }.reverse > > i just basically want my top 10 articles, do i need to plug in :limit => > 10 someplace? > > thanks! > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
baldwina-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-11 16:30 UTC
Re: just want my first ten parts of the hash? how to chop it?
Something like this should do it: @temp = Articles.find(:all, :order => ''articles.rating desc'', :limit => 10) It''s much faster to combine the sorting with the database query than to pull all the records and sort them in code. Aaron On Dec 11, 9:23 am, koloa <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have this > > @temp = Articles.find(:all) > @temp.sort_by { | articles | articles.rating }.reverse > > i just basically want my top 10 articles, do i need to plug in :limit => > 10 someplace? > > thanks! > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2006-Dec-11 16:34 UTC
Re: just want my first ten parts of the hash? how to chop it?
On Dec 11, 2006, at 11:23 AM, koloa wrote:> I have this > > @temp = Articles.find(:all) > @temp.sort_by { | articles | articles.rating }.reverse > > i just basically want my top 10 articles, do i need to plug > in :limit => > 10 someplace? > > thanks!Depending on whether rating is an attribute or not, one of these should work: @temp = Articles.find(:all, :order => ''rating DESC'', :limit => 10) Or (assuming rating is numeric, note the negation): @temp = Articles.find(:all).sort_by {|article| - article.rating }.first(10) And you aren''t getting a ''hash'' you''re getting an Array from your Articles.find call. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
hi thanks all. question about the statement... the order option orders first and then limits it? or does it limit the first 10 itmes in the table and then orders that selection? unknown wrote:> Something like this should do it: > > @temp = Articles.find(:all, :order => ''articles.rating desc'', :limit => > 10) > > It''s much faster to combine the sorting with the database query than to > pull all the records and sort them in code. > > Aaron-- 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 -~----------~----~----~----~------~----~------~--~---
opps, i also forgot to add that rating is from the acts_as_rateable plugin. where my article table acts_as_rateable. koloa wrote:> > hi thanks all. > > question about the statement... > > the order option orders first and then limits it? or does it limit the > first 10 itmes in the table and then orders that selection? > > > > > > unknown wrote: >> Something like this should do it: >> >> @temp = Articles.find(:all, :order => ''articles.rating desc'', :limit => >> 10) >> >> It''s much faster to combine the sorting with the database query than to >> pull all the records and sort them in code. >> >> Aaron-- 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-11 16:58 UTC
Re: just want my first ten parts of the hash? how to chop it
Hi -- On Mon, 11 Dec 2006, koloa wrote:> unknown wrote: >> Something like this should do it: >> >> @temp = Articles.find(:all, :order => ''articles.rating desc'', :limit => >> 10) >> >> It''s much faster to combine the sorting with the database query than to >> pull all the records and sort them in code. >> >> Aaron > > > hi thanks all. > > question about the statement... > > the order option orders first and then limits it? or does it limit the > first 10 itmes in the table and then orders that selection?The find method is basically just constructing a query like this: SELECT * from articles ORDER BY rating DESC LIMIT 10; and the database will always order first. David -- Q. What''s a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---
baldwina-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-11 17:41 UTC
Re: just want my first ten parts of the hash? how to chop it
On Dec 11, 9:39 am, koloa <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> opps, i also forgot to add that rating is from the acts_as_rateable > plugin. where my article table acts_as_rateable.So the rating is stored in another table. In that case you want to do a SQL join that combines Articles with Ratings. Just add a :include to your find statement like this: @temp = Articles.find(:all, :order => ''ratings_table_name.rating desc'', :limit => 10, :include => :rating) One thing that can be a little confusing is that the :order parameter requires the name of the table you are referring to, while :include references the name of the model association. That''s because the :order parameter gets dropped right into the SQL statement while the :include is processed by rails and converted into SQL. Aaron --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---