Override has_many :order at run-time?

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

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

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.

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’) %>

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.” :slight_smile:

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.