Ruby on Rails
HowToCreateOrderedAssociations

Imagine you have a Post class, which can have many Comment objects. You want the associated Comments to be ordered by the date they were posted on.

To do this, add a posted_on field, then define your Associations like this:


class Post < ActiveRecord::Base
  has_many :comments, :order => "posted_on" 
  ...
end

For more information see the ActiveRecord documentation for associations documentation