# Override has\_many :order at run-time?

**URL:** <https://www.ruby-forum.com/t/override-has-many-order-at-run-time/97454>\
**Category:** Rails\
**Created:** [January 26, 2006, 1:41am UTC](https://www.ruby-forum.com/t/override-has-many-order-at-run-time/97454 "2006-01-26T01:41:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 26, 2006, 1:41am UTC](https://www.ruby-forum.com/t/override-has-many-order-at-run-time/97454/1 "2006-01-26T01:41:33Z")

</div>

Is it possible to override has\_many’s :order attribute at run-time? e.g.  
something like:

\<% for category in @site.categories(:order=\>‘name’) %\>  
…

\<% for category in @site.categories(:order=\>‘rank’) %\>  
…

thanks  
csn

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 26, 2006, 1:49am UTC](https://www.ruby-forum.com/t/override-has-many-order-at-run-time/97454/2 "2006-01-26T01:49:42Z")

</div>

csn wrote:

> Is it possible to override has\_many’s :order attribute at run-time? e.g.  
> something like:
> 
> \<% for category in @site.categories(:order=\>‘name’) %\>  
> …
> 
> \<% for category in @site.categories(:order=\>‘rank’) %\>  
> …
> 
> thanks  
> csn

\<% for category in @site.categories.find(:all, :order=\>‘name’) %\>

Gokhan A.  
Sylow web development :: [www.sylow.net](http://www.sylow.net)

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 26, 2006, 3:31am UTC](https://www.ruby-forum.com/t/override-has-many-order-at-run-time/97454/3 "2006-01-26T03:31:28Z")

</div>

On Jan 25, 2006, at 6:04 PM, Bob S. wrote:

> Does ActiveRecord only load the data once and keep it cached  
> internally?
> 
> ie: would this only hit the db once?
> 
> \<% for category in @site.categories.find(:all, :order=\>‘name’) %\>
> 
> \<% for category in @site.categories.find(:all, :order=\>‘rank’) %\>

Check the development log, and you shall have the answer directly.

I’m quite sure the answer is no.

You could put this loop into a partial, render it to a string in  
the controller, and reference the string in the view twice.

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 26, 2006, 3:04am UTC](https://www.ruby-forum.com/t/override-has-many-order-at-run-time/97454/4 "2006-01-26T03:04:19Z")

</div>

Does ActiveRecord only load the data once and keep it cached internally?

ie: would this only hit the db once?

\<% for category in @site.categories.find(:all, :order=\>‘name’) %\>

\<% for category in @site.categories.find(:all, :order=\>‘rank’) %\>

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 26, 2006, 3:40am UTC](https://www.ruby-forum.com/t/override-has-many-order-at-run-time/97454/5 "2006-01-26T03:40:32Z")

</div>

On Jan 25, 2006, at 6:29 PM, Tom M. wrote:

> Check the development log, and you shall have the answer directly.
> 
> I’m quite sure the answer is no.
> 
> You could put this loop into a partial, render it to a string in  
> the controller, and reference the string in the view twice.

Oops, did not notice the orders were different!

So…make that “The answer is no.” 🙂

You could grab the categories in the controller, then use a Ruby  
sort in the view…

controller:

@categories = @site.categories

view:

\<% for category in @categories.sort { |a,b| a.rank \<=\> b.name } %\>

\<% for category in @categories.sort { |a,b| a.rank \<=\> b.rank } %\>

–  
– Tom M.
