Ordering the many in a many to one relationship

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”)

Hi Nick –

On 2-Aug-06, at 11:43 AM, Nick Jones wrote:

= ?", params[:id]], :order => “name”)
In the Category model, add an order to the association:

class Category < ActiveRecord::Base
has_many :brands, :order => ‘name’



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