Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6191191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:43:13+00:00 2026-05-24T02:43:13+00:00

I’m trying to find the best way to define models for team / member

  • 0

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:

  1. 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
    
  2. 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:43:14+00:00Added an answer on May 24, 2026 at 2:43 am

    I ended up going with the following approach using the explicit foreign_key which is working OK.

    class Team < ActiveRecord::Base
      has_many :members
      has_many :people, :through => :members
      belongs_to :head_coach, :class => 'Person', :foreign_key head_coach_id 
      belongs_to :assistant_coach, :class => 'Person', :foreign_key assistant_coach_id
    end
    
    class Person < ActiveRecord::Base
      has_many :teams, :through => :members
    
    end
    
    class Member < ActiveRecord::Base
      belongs_to :team
      belongs_to :person
      # has a "position" which is a string
    end
    

    The only downside to date is I haven’t been able to get the foreign keys to load automatically in the controller. I need to add code such as the following in order to save the associations:

    # POST /teams
    def create
      @team = Team.new
      @team.head_coach = Person.find(params[:head_coach])
      @team.assistant_coach = Person.find(params[:assistant_coach])
    
      ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.