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!
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!
Articles.find(:all, :order => ‘publication_date DESC’, :limit => 10 )
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?
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
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
On Dec 11, 2006, at 11:23 AM, koloa wrote:
I have this
@temp = Articles.find(:all)
@temp.sort_by { | articles | articles.rating }.reversei 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
Hi –
On Mon, 11 Dec 2006, 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?
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 (Ruby for Rails)
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)
On Dec 11, 9:39 am, koloa [email protected] 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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs