Lets say I have an owner record which has_many employees. If I want to eager load the employees then Owner.find(:all, :include => :employee) works fine and dandy. What if I only wanted to include male employees in the eager loading for instance? Is there an elegant and simple way to eager load with a condition applied to the included model? Have google and API''d around but can''t see anything obvious. any ideas? Many thanks RJ -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Owner.find(:all, :include => :employee, :conditions => ''employee.sex = "male"'') should work. Paolo On 16/01/07, RJ <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Lets say I have an owner record which has_many employees. > > If I want to eager load the employees then Owner.find(:all, :include => > :employee) works fine and dandy. > > What if I only wanted to include male employees in the eager loading for > instance? Is there an elegant and simple way to eager load with a > condition applied to the included model? > > Have google and API''d around but can''t see anything obvious. > > any ideas? Many thanks > > RJ > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Also, if this is called from multiple places in your application, you can simplify it by adding associations to your model: class Owner < ActiveRecord::Base has_many :employees has_many :male_employees, :class_name => ''Employee'', :conditions => ''gender = "M"'' has_many :female_employees, :class_name => ''Employee'', :conditions => ''gender = "F"'' end which lets you call: Owner.find(:all, :include => :male_employees) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---