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 -- Posted via http://www.ruby-forum.com/.
On Sun Jul 02, 2006 at 09:48:17AM +0200, jeff wrote:> 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?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..> > thanks, > > jeff > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
<% @categories.each do |category| %> <%= render :partial => ''sublist'', :collection => category.subcategories.sort_by(&:position) %> <% end %> That should do it. -Jonathan. On 7/2/06, jeff <jeff@jeffie.com> wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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 -- Posted via http://www.ruby-forum.com/.
On 7/2/06, jeff <jeff@jeffie.com> wrote:> > 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 %>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> view or do i have to access this through my controller? > > thanks, > > jeff > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060702/6de5ea89/attachment.html