Find across multiple levels of relationships

I was writing some code today and I would like to be able to find all my users who work for a specific group. The relationship is that a User belongs to one or more Jobs. Each Job belongs to a Group.

user.rb
class User “jobUsers”, :association_foreign_key => “jobID”,
:foreign_key => “usrID”

def self.get_active_by_group(groupname)
find(:all,
:include => [:jobs, {:jobs =>:group}],
:conditions => ["usrDeleted = 'false' and groups.grpName=?", groupname],
:order => “usrName ASC”)
end
end

job.rb
class Job ‘jobGrpID’
end

The key to this working is in the :include parameter.  (Sorry I can never get wordpress to format code that I cut and paste correctly)

Leave a Reply