views:

41

answers:

1

Hi folks,

I have a simple many-to-many E-R described as below:

Model order.rb:

class Order < ActiveRecord::Base
  has_many :cronologies
  has_many :statuses, :through => :cronologies
end

Model cronology.rb:

class Cronology < ActiveRecord::Base
  belongs_to :order
  belongs_to :status
  validates_uniqueness_of :order_id, :scope => :status_id
end

Model status.rb:

class Status < ActiveRecord::Base
  has_many :cronologies
  has_many :orders, :through => :cronologies
end

This code below lets me get all statuses assigned to an order.

@order.statuses

...but how to get statuses ordered by the "created_at" attribute of the cronology table?

+4  A: 
@order.statuses.all(:order => "cronologies.created_at")

or put it into association if you always want it ordered this way.

class Order < ActiveRecord::Base
  has_many :cronologies
  has_many :statuses, :through => :cronologies, :order => "cronologies.created_at"
end
uzzz