Forum: Ruby on Rails Override has_many :order at run-time?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
csn (Guest)
on 2006-01-26 02:41
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
Gokhan A. (Guest)
on 2006-01-26 02:49
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
Bob S. (Guest)
on 2006-01-26 04:04
(Received via mailing list)
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') %>
Tom M. (Guest)
on 2006-01-26 04:31
(Received via mailing list)
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.
Tom M. (Guest)
on 2006-01-26 04:40
(Received via mailing list)
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.
This topic is locked and can not be replied to.