I have two models in a has_many relationship such that Log has_many Items. Rails then nicely sets up things like: some_log.items which returns all of the associated items to some_log. If I wanted to order these items based on a different field in the Items model is there a way to do this through a similar construct, or does one have to break down into something like:
Item.find_by_log_id(:all,some_log.id => "some_col DESC")
There are multiple ways to do this:
If you want all calls to that association to be ordered that way, you can specify the ordering when you create the association, as follows:
You could also do this with a named_scope, which would allow that ordering to be easily specified any time Item is accessed:
If you always want the items to be ordered in the same way by default, you can use the (new in Rails 2.3) default_scope method, as follows: