: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


Owner.find(:all, :include => :employee, :conditions => ‘employee.sex =

should work.


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”’

which lets you call:

Owner.find(:all, :include => :male_employees)