# Help with sorting / ordering

**URL:** <https://discuss.rubyonrails.org/t/help-with-sorting-ordering/40412>\
**Category:** rubyonrails-talk\
**Created:** [March 13, 2009, 9:51pm UTC](https://discuss.rubyonrails.org/t/help-with-sorting-ordering/40412 "2009-03-13T21:51:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Z\_Meinlschmidt](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@Tom\_Z\_Meinlschmidt](https://discuss.rubyonrails.org/u/Tom_Z_Meinlschmidt)\
**Post date:** [March 13, 2009, 9:51pm UTC](https://discuss.rubyonrails.org/t/help-with-sorting-ordering/40412/1 "2009-03-13T21:51:17Z")

</div>

easiest and fastest way is to keep number of reviews in column in product table... then simple add :order =\> 'reviews\_count desc' in your Product.find()

tom

Robert Scott wrote:

---

<div class="post-metadata">

**Author:** ![Kaspars\_Bankovskis](https://avatars.discourse-cdn.com/v4/letter/k/cab0a1/32.png) [@Kaspars\_Bankovskis](https://discuss.rubyonrails.org/u/Kaspars_Bankovskis)\
**Post date:** [March 13, 2009, 10:00pm UTC](https://discuss.rubyonrails.org/t/help-with-sorting-ordering/40412/2 "2009-03-13T22:00:27Z")

</div>

and don't forget to add to reviews model: belongs\_to :product, :counter\_cache =\> true

---

<div class="post-metadata">

**Author:** ![Tom\_Z\_Meinlschmidt](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@Tom\_Z\_Meinlschmidt](https://discuss.rubyonrails.org/u/Tom_Z_Meinlschmidt)\
**Post date:** [March 13, 2009, 10:18pm UTC](https://discuss.rubyonrails.org/t/help-with-sorting-ordering/40412/3 "2009-03-13T22:18:47Z")

</div>

OK. so then you need to create your own queries with Reviews.find\_by\_sql() and insert appropriate sql statement, eg

select product\_id, count(\*) as counter from reviews where age between (10,20) and ... order by 2 desc

tom

Robert Scott wrote:
