Topic: Sort Order with Relationships

Hi,

I've got a couple models for orders and lines.

class Order < ActiveRecord::Base
    has_many :lines
end

class Line < ActiveRecord::Base
    belongs_to :order
end

When I try to do something like:

@order = Order.find_by_id(params[:id])
@lines = @order.lines

What is the most efficient way to sort @lines? I'd like to sort it by the product and cost columns in the lines table. Thanks,

/DB

Re: Sort Order with Relationships

You can do this:

@order = Order.find(params[:id])
@lines = @order.lines.find(:all, :order => 'product, cost')

Railscasts - Free Ruby on Rails Screencasts

Re: Sort Order with Relationships

This is perfect - thanks!