I have a many-to-one relationship between a model named Category(one) and a model named Brands (one). How can I get ActiveRecord to order the brands by the "name" attribute? This seems like a simple question and I can think of long ways around it but there has go to be a way to do this by accessing @category.brands . Can anyone help? I tried the code below in the controller but it didn''t seem to order the brands in the view when I accessed @category.brands @category = Category.find(params[:id]) @category.brands = Brand.find(:all, :conditions => ["category_id = ?", params[:id]], :order => "name") -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060802/af252ac8/attachment.html
Jeffrey Hardy
2006-Aug-02 16:19 UTC
[Rails] Ordering the many in a many to one relationship
Hi Nick -- On 2-Aug-06, at 11:43 AM, Nick Jones wrote:> I have a many-to-one relationship between a model named Category > (one) and a model named Brands (one). How can I get ActiveRecord to > order the brands by the "name" attribute? This seems like a simple > question and I can think of long ways around it but there has go to > be a way to do this by accessing @category.brands . Can anyone > help? I tried the code below in the controller but it didn?t seem > to order the brands in the view when I accessed @category.brands > > @category = Category.find(params[:id]) > @category.brands = Brand.find(:all, :conditions => ["category_id > = ?", params[:id]], :order => "name")In the Category model, add an order to the association: class Category < ActiveRecord::Base has_many :brands, :order => ''name'' end /Jeff -- http://re.visioni.st http://quotedprintable.com