Forum: Ruby on Rails Where clause for a has_many

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Jason (Guest)
on 2007-04-03 21:45
I have two models, History and SalesOrder, which are setup like this:

class History < ActiveRecord::Base

   belongs_to :sales_order


class SalesOrder < ActiveRecord::Base

   has_many :histories
   has_many :latest_history,
            :class_name => "History",
            :order => "id desc"


Is it possible to use a where clause when finding SalesOrders that is
based on History?  For example:

orders = SalesOrder.find(:all, :order => 'latest_history asc')

Is that kind of thing even possible with Active Record?
Ben M. (Guest)
on 2007-04-04 00:59
(Received via mailing list)
I think you want to pass has_many a block with your conditions...

Read about it here:

This topic is locked and can not be replied to.