# Order\_by with property

**URL:** <https://www.ruby-forum.com/t/order-by-with-property/69724>\
**Category:** Rails\
**Created:** [September 23, 2006, 11:08am UTC](https://www.ruby-forum.com/t/order-by-with-property/69724 "2006-09-23T11:08:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![huli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d07c76/32.png) [@huli](https://www.ruby-forum.com/u/huli)\
**Post date:** [September 23, 2006, 11:08am UTC](https://www.ruby-forum.com/t/order-by-with-property/69724/1 "2006-09-23T11:08:34Z")

</div>

I have an usual pagination in rails. But I am trying to sort by a  
property which is not in the same table on the database. (I have an  
Entry class which has a region\_id. Then I got a Region class with id and  
name. Now I would like to sort the Entries in my Pagination with the  
name of the region.)

If I use a collection I have to load the whole bunch and therefore this  
is not an option. Do I need to do a find\_by\_sql? Or do I miss anything?

thanks  
h.

---

<div class="post-metadata">

**Author:** ![huli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d07c76/32.png) [@huli](https://www.ruby-forum.com/u/huli)\
**Post date:** [September 23, 2006, 12:38pm UTC](https://www.ruby-forum.com/t/order-by-with-property/69724/2 "2006-09-23T12:38:01Z")

</div>

huli wrote:

> I have an usual pagination in rails. But I am trying to sort by a  
> property which is not in the same table on the database. (I have an  
> Entry class which has a region\_id. Then I got a Region class with id and  
> name. Now I would like to sort the Entries in my Pagination with the  
> name of the region.)
> 
> If I use a collection I have to load the whole bunch and therefore this  
> is not an option. Do I need to do a find\_by\_sql? Or do I miss anything?

You can pass all the options you would usually pass to find to paginate,  
so in this case you could use :joins =\> “INNER JOIN regions on  
regions.id = region\_id” and then you can :order =\> ‘regions.name’

Fred

---

<div class="post-metadata">

**Author:** ![huli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d07c76/32.png) [@huli](https://www.ruby-forum.com/u/huli)\
**Post date:** [September 23, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/order-by-with-property/69724/3 "2006-09-23T15:56:42Z")

</div>

Fred wrote:

> huli wrote:
> 
> > I have an usual pagination in rails. But I am trying to sort by a  
> > property which is not in the same table on the database. (I have an  
> > Entry class which has a region\_id. Then I got a Region class with id and  
> > name. Now I would like to sort the Entries in my Pagination with the  
> > name of the region.)
> > 
> > If I use a collection I have to load the whole bunch and therefore this  
> > is not an option. Do I need to do a find\_by\_sql? Or do I miss anything?
> 
> You can pass all the options you would usually pass to find to paginate,  
> so in this case you could use :joins =\> “INNER JOIN regions on  
> regions.id = region\_id” and then you can :order =\> ‘regions.name’
> 
> Fred

Cool thats it! Thanks a lot!

h.
