Override has_many :order at run-time?


#1

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


#2

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


#3

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.


#4

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


#5

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.