Hi Lee,
You can extend associations in ActiveRecord:
class User
has_many :locations do
def employees
# Use whatever logic you'd like here.
locations.find(:all, :include => [:employees]).collect {|l| l.employees }
end
end
end
u = User.find 1
u.locations.employees #=> calls our method defined above
And see this:
http://ryandaigle.com/articles/2006/12/3/extend-your-activerecord-association-methods
You may also try has_many :through
:
class User
has_many :user_locations
has_many :locations, :through => :user_locations
# Not sure if you can nest this far, this guy has problems with it:
# http://tim.theenchanter.com/2008/10/how-to-hasmany-through-hasmany-through.html
#
# Maybe if locations was a habtm instead of :through? experiment with it!
#
has_many :employees, :through => :locations
end
u = User.find 1
u.employees #=> Uses associations to figure out the SQL
In general, Lee, I'm concerned about your data model. HABTM relationships are not really recommended now. Using has_many :through
allows you to name the join table, and then you can store attributes on a relationship with better business meaning. I would say the "Railsy" thing is to add some through relationships to expose more domain modelling.
Also, some example models would be helpful to really understand your question.
Good luck!