# Help with sorting / ordering

**URL:** <https://www.ruby-forum.com/t/help-with-sorting-ordering/162806>\
**Category:** Rails\
**Created:** [March 13, 2009, 10:40pm UTC](https://www.ruby-forum.com/t/help-with-sorting-ordering/162806 "2009-03-13T22:40:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [March 13, 2009, 10:40pm UTC](https://www.ruby-forum.com/t/help-with-sorting-ordering/162806/1 "2009-03-13T22:40:20Z")

</div>

Hi! I hope someone can offer some insight or direction into a problem  
I’m having.

In my application, I have two tables: products and reviews.

- Products stores information on things that are sold
- Reviews store comments from users that purchased the product, as well  
as boolean field about if they would purchase again.

The models are setup so that products has\_many reviews and reviews  
belong\_to their respective products.

So my question is this:  
How can I structure a query so that I can order products based on the  
number of people who would purchase it again?

It’d be alot easier for me, and probably my server, if I just kept a  
running count of those in the actual product record, but I’m trying to  
keep it dynamic so that I can create custom reviewer sets elsewhere.

Thanks in advance for any direction.

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [March 13, 2009, 10:54pm UTC](https://www.ruby-forum.com/t/help-with-sorting-ordering/162806/2 "2009-03-13T22:54:09Z")

</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 S. wrote:

> belong\_to their respective products.
> 
> So my question is this:  
> How can I structure a query so that I can order products based on the  
> number of people who would purchase it again?
> 
> It’d be alot easier for me, and probably my server, if I just kept a  
> running count of those in the actual product record, but I’m trying to  
> keep it dynamic so that I can create custom reviewer sets elsewhere.
> 
> Thanks in advance for any direction.

# –

Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

# [www.meinlschmidt.com](http://www.meinlschmidt.com) www.maxwellrender.cz www.lightgems.cz

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [March 13, 2009, 11:13pm UTC](https://www.ruby-forum.com/t/help-with-sorting-ordering/162806/3 "2009-03-13T23:13:16Z")

</div>

Right, but that approach also means that I will be using all reviews for  
all users.

The only reason I’m going through the hassle of trying to make it  
dynamic is so that I could further segment the reviewers. So instead of  
saying X reviewers would buy it again, I could get closer to X reviewers  
in your age group would buy it again or X reviewers in your area, etc.  
etc.

Tom Z Meinlschmidt wrote:

> 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 S. wrote:
> 
> > belong\_to their respective products.
> > 
> > So my question is this:  
> > How can I structure a query so that I can order products based on the  
> > number of people who would purchase it again?
> > 
> > It’d be alot easier for me, and probably my server, if I just kept a  
> > running count of those in the actual product record, but I’m trying to  
> > keep it dynamic so that I can create custom reviewer sets elsewhere.
> > 
> > Thanks in advance for any direction.
> 
> # –
> 
> Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
> 
> # [www.meinlschmidt.com](http://www.meinlschmidt.com) www.maxwellrender.cz www.lightgems.cz

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [March 13, 2009, 11:21pm UTC](https://www.ruby-forum.com/t/help-with-sorting-ordering/162806/4 "2009-03-13T23:21:40Z")

</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 S. wrote:

> > > 
> > 
> > # –
> > 
> > Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
> > 
> > # [www.meinlschmidt.com](http://www.meinlschmidt.com) www.maxwellrender.cz www.lightgems.cz

# –

Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

# [www.meinlschmidt.com](http://www.meinlschmidt.com) www.maxwellrender.cz www.lightgems.cz

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [March 14, 2009, 1:44pm UTC](https://www.ruby-forum.com/t/help-with-sorting-ordering/162806/5 "2009-03-14T13:44:19Z")

</div>

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

On Mar 13, 11:51 pm, Tom Z Meinlschmidt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
