:include with conditions

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

Owner.find(:all, :include => :employee, :conditions => ‘employee.sex =
“male”’)

should work.

Paolo

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)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs