I'm trying to find the best way to define models for team / member / person, where a person can be a member of many teams and a team can have many members. Along with the "member" relationship is the position which the person fills for the team. A team should also have exactly one head coach and one assistant coach. A person could be a head/assistant coach for more than one team.
Below is my current (futile) attempt:
class Team < ActiveRecord::Base
has_many :members
has_many :people, :through => :members
belongs_to :head_coach :class => 'Person'
belongs_to :assistant_coach :class => 'Person'
end
class Person < ActiveRecord::Base
has_many :teams
has_many :teams, :through => :members
end
class Member < ActiveRecord::Base
belongs_to :team
belongs_to :person
# has a "position" which is a string
end
This approach is causing me two problems:
The Team's belongs_to :head_coach and :assistant_coach doesn't work. Maybe it should be a has_one, but then I'm not sure it makes sense to put the belongs_to in Person (I want a FK in Team to Person). The example below shows that how I have it set-up doesn't jive with ActiveRecord:
irb(main):006:0> t = Team.find(1) => #<Team id: 1, name: "Champs", created_at: "2011-07-18 01:50:56", updated_at: "2011-07-19 01:47:26", head_coach: nil> irb(main):007:0> t.head_coach => nil irb(main):008:0> t.head_coach = Person.find(1) => #<Person id: 1, name: "Chris", created_at: "2011-07-18 01:52:34", updated_at: "2011-07-18 01:52:34"> irb(main):009:0> t.save => true irb(main):010:0> t.head_coach => #<Person id: 1, name: "Chris", created_at: "2011-07-18 01:52:34", updated_at: "2011-07-18 01:52:34"> irb(main):011:0> Team.find(1).head_coach => nil
The has_many :through seems to work but I haven't found a good way to list the positions for each person within a team. This is my current attempt within a view:
<% @team.people.each do |person| %> <%= person.name +" "+ @team.members.find_by_person_id(person).position %>
Is there an overall better approach to representing these relationships?
Thanks for the help!
-Chris