Hi all.
I am currently thinking about how to do the following:
I have the following models.
Team
Person
Team has various positions (manager, programmer etc.) I would like each
one of those positions to reference one or more Person records. A single
Person record could be on 6 different teams at the same time in
different roles. the same person could even be on the same team multiple
times in different roles.
I am not 100% sure how to go about this. I am thinking that I will need
something like an intermediate record.
teams_people
id
team_id
people_id
role
I would then have (I think) to have methods in the Team model that will
find each person in the @team.people array based on the role.
I the end I want a view to have something like:
Team Name
Manager: Person1, Person 4
Programmer: Person2, Person 3
Designer: Person2
Lackey: Person3
With just a single find that :includes all the relevant people.
I apologise if this isn’t very clear. The joys of not being a Ruby
programmer but a Ruby dabbler
RJ
RurouniJones wrote:
Hi all.
I am currently thinking about how to do the following:
I have the following models.
Team
Person
Team has various positions (manager, programmer etc.) I would like each
one of those positions to reference one or more Person records. A single
Person record could be on 6 different teams at the same time in
different roles. the same person could even be on the same team multiple
times in different roles.
I am not 100% sure how to go about this. I am thinking that I will need
something like an intermediate record.
teams_people
id
team_id
people_id
role
I would then have (I think) to have methods in the Team model that will
find each person in the @team.people array based on the role.
I the end I want a view to have something like:
Team Name
Manager: Person1, Person 4
Programmer: Person2, Person 3
Designer: Person2
Lackey: Person3
With just a single find that :includes all the relevant people.
You’re definitely on the right track. I’d suggest naming the join model
something like Membership (table name ‘memberships’ instead of
‘teams_people’), which is the way to go when using has_many :through
instead of habtm. Your use of the ‘role’ field is exactly what I’d do.
Here’s something I wrote that talks about working with this kind of join
model and shows how you can use that to create convenient specialized
accessors for the various roles:
http://blog.hasmanythrough.com/articles/2006/05/06/through_gets_uniq
I haven’t played around with using :include for that kind of setup, but
I think you should just be able to :include the various specialized
associations and it will do the right thing.
–
Josh S.
http://blog.hasmanythrough.com
Josh S. wrote:
I haven’t played around with using :include for that kind of setup, but
I think you should just be able to :include the various specialized
associations and it will do the right thing.
–
Josh S.
http://blog.hasmanythrough.com
Ye gods!
Thank you very much for the link. It is a stupendously good tutorial for
this sort of thing. Thank you very much.
Do you have plans on adding that to the wiki.rubyonrails site? (Or
create a page that then links to the blog?) I use the Wiki site
extensively (as I imagine others do to) and a page that has this
information would be great.
Many thanks
RJ