# Dynamic order on has\_many relationship?

**URL:** <https://www.ruby-forum.com/t/dynamic-order-on-has-many-relationship/51098>\
**Category:** Rails\
**Created:** [December 26, 2005, 8:14am UTC](https://www.ruby-forum.com/t/dynamic-order-on-has-many-relationship/51098 "2005-12-26T08:14:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Nolan.ca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8edcca/32.png) [@Chris\_Nolan.ca](https://www.ruby-forum.com/u/Chris_Nolan.ca)\
**Post date:** [December 26, 2005, 8:14am UTC](https://www.ruby-forum.com/t/dynamic-order-on-has-many-relationship/51098/1 "2005-12-26T08:14:28Z")

</div>

I was wondering if it’s possible to some how control the :order given  
on a has\_many depending on another attribute?

Or do I need to just create multiple has\_many relationships pointing  
to the same table but with different orders and then select which one  
I use in the controller instead?

e.g

class Category \< AR::B  
has\_many :products, :order =\> //something magical?//  
end

class Category \< AR:B  
has\_many :products, :order =\> “product\_id”  
has\_many :products\_by\_desc, :class\_name =\> “Product”, :order =\>  
“description”  
has\_many :products\_by\_gtin, :class\_name =\> “Product”, :order =\> “gtin”  
end

If I had a column that was default\_order on the Category, could it  
evaluate it and set the :order at the model level, or do I go in and  
get my record set and hardcode which relationship matches which value  
of the column?

Thanks,  
Chris Nolan.ca  
[http://kekova.ca/](http://kekova.ca/)

---

<div class="post-metadata">

**Author:** ![Chris\_Nolan.ca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8edcca/32.png) [@Chris\_Nolan.ca](https://www.ruby-forum.com/u/Chris_Nolan.ca)\
**Post date:** [December 26, 2005, 10:35am UTC](https://www.ruby-forum.com/t/dynamic-order-on-has-many-relationship/51098/2 "2005-12-26T10:35:45Z")

</div>

Chris Nolan.ca wrote:

> I was wondering if it’s possible to some how control the :order given  
> on a has\_many depending on another attribute?

I have been wondering about the same. It did occur to me that when you  
are using a has\_many you aren’t really enforcing some relationship so  
much as adding a convenience finder method. So it is probably just as  
valid to create some function which runs find\_by\_sql and returns the  
results you are after? There are some convenience methods added as a  
result of has\_many, so you may want to check you don’t rely on those.

Local sorting of your object is probably also possible (or can be  
emulated)

In my case I wanted to have subsets of the has\_many relation, eg find  
all completed orders, find all orders, find orders not yet shipped, etc,  
etc. Grateful for any better suggestions on doing this than just adding  
methods to the model which return the appropriate results?

Ed W

---

<div class="post-metadata">

**Author:** ![Chris\_Nolan.ca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8edcca/32.png) [@Chris\_Nolan.ca](https://www.ruby-forum.com/u/Chris_Nolan.ca)\
**Post date:** [December 26, 2005, 4:25pm UTC](https://www.ruby-forum.com/t/dynamic-order-on-has-many-relationship/51098/3 "2005-12-26T16:25:06Z")

</div>

> class Category \< AR::B  
> has\_many :products  
> end

The reason you can’t put anything in the has\_many() method call is  
it’s executed when the class is loaded. Luckily you can use the full  
power of the find() method.

@category.products.find(:all, :order =\> ‘something magical’)

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)
