:order in partial

Hi,
i was looking through the docs and couldn’t find a good clean solution
for ordering a partial with an :order clause.

say i was iterating over categories and thier corresponding
sub-categories (regular belongs to) and wanted to order the
subcategories according to a certain column;

<% for category in @categories %>
<% render: partial => ‘sublist’, :collection =>
category.subcategories, :order => ‘position’ %> # position is a
column in my subcategories table
<% end %>

this isn’t really working; is there a different way to do this in the
view or do i have to access this through my controller?

thanks,

jeff

On Sun Jul 02, 2006 at 09:48:17AM +0200, jeff wrote:

category.subcategories, :order => ‘position’ %> # position is a
column in my subcategories table
<% end %>

this isn’t really working; is there a different way to do this in the
view or do i have to access this through my controller?

you should definitely read david a black’s book (manning.com i think) .
in summary, you can sort or find within the result array with a block…

<% @categories.each do |category| %>
<%= render :partial => ‘sublist’, :collection =>
category.subcategories.sort_by(&:position) %>
<% end %>

That should do it.

-Jonathan.

On 7/2/06, jeff [email protected] wrote:

<% render: partial => 'sublist', :collection =>

category.subcategories, :order => ‘position’ %> # position is a
column in my subcategories table
<% end %>

You can use find on the associations in a couple of places. You could
specify the order explicitly for the partial
<%= render :partial=>‘sublist’, :collection =>
category.sucategories.find(:all,
:order => ‘position’) %>

Or include the order in the has_many deleration

has_many :subcategories, :order => ‘position’

Either one should work.
Johnathans approach should work also.
(Repeat)
:collection => category.subcategories.sort_by(&:position) %>

this isn’t really working; is there a different way to do this in the

i looked up in rubycentral.com (couldn’t buy the rails book just yet)
and i found a arr.sort function that fit my needs exactly… i ended up
doing

for subcat in category.subcategories.sort {|subcat1, subcat2|
subcat1.position <=> subcat2.position}

and i got a perfect order in the subcat output list.
thanks for your help…

jeff

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs