[Rails] Re: Override has_many :order at run-time?

Tom Mornini tmornini at infomania.com
Thu Jan 26 02:38:52 GMT 2006


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

> On Jan 25, 2006, at 6:04 PM, Bob Silva 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.


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 Mornini



More information about the Rails mailing list