Forum: Ruby on Rails Where clause for a has_many

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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

end

class SalesOrder < ActiveRecord::Base

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

end


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:

http://api.rubyonrails.org/classes/ActiveRecord/As...

b
This topic is locked and can not be replied to.