# Just want my first ten parts of the hash? how to chop it?

**URL:** <https://www.ruby-forum.com/t/just-want-my-first-ten-parts-of-the-hash-how-to-chop-it/79020>\
**Category:** Rails\
**Created:** [December 11, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/just-want-my-first-ten-parts-of-the-hash-how-to-chop-it/79020 "2006-12-11T17:23:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [December 11, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/just-want-my-first-ten-parts-of-the-hash-how-to-chop-it/79020/1 "2006-12-11T17:23:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [December 11, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/just-want-my-first-ten-parts-of-the-hash-how-to-chop-it/79020/2 "2006-12-11T17:31:52Z")

</div>

Articles.find(:all, :order =\> ‘publication\_date DESC’, :limit =\> 10 )

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [December 11, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/just-want-my-first-ten-parts-of-the-hash-how-to-chop-it/79020/3 "2006-12-11T17:31:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [December 11, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/just-want-my-first-ten-parts-of-the-hash-how-to-chop-it/79020/4 "2006-12-11T17:34:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [December 11, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/just-want-my-first-ten-parts-of-the-hash-how-to-chop-it/79020/5 "2006-12-11T17:39:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [December 11, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/just-want-my-first-ten-parts-of-the-hash-how-to-chop-it/79020/6 "2006-12-11T17:35:53Z")

</div>

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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [December 11, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/just-want-my-first-ten-parts-of-the-hash-how-to-chop-it/79020/7 "2006-12-11T17:59:59Z")

</div>

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](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](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [December 11, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/just-want-my-first-ten-parts-of-the-hash-how-to-chop-it/79020/8 "2006-12-11T18:43:14Z")

</div>

On Dec 11, 9:39 am, koloa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
