title: How to structure relationships between models in Ruby On Rails
body: <p>I'm developing an application to schedule soccer matches for two teams at a time. So, for example I need to schedule a match for two teams, but I'm not sure how can I structure the database relation between these two entities.</p>
  
  <p>One solution I thought is a relation many to many between them, and in the Associative Table(match_teams) I put a is_visitor field, to indicate which team is playing outside home:</p>
  
  <p>team model:</p>
  
  <pre><code>class Team &lt; ApplicationRecord
      has_many :match_team
      has_many :matches, through: :match_team
  end
  </code></pre>
  
  <p>match model:</p>
  
  <pre><code>class Match &lt; ApplicationRecord
      has_many :match_team
      has_many :teams, through: :match_team
  end
  </code></pre>
  
  <p>match_team model:</p>
  
  <pre><code>class MatchTeam &lt; ApplicationRecord
    belongs_to :match
    belongs_to :team
  end
  </code></pre>
  
  <p>team migration:</p>
  
  <pre><code>class CreateTeams &lt; ActiveRecord::Migration[5.1]
    def change
      create_table :teams do |t|
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p>match migration:</p>
  
  <pre><code>class CreateMatches &lt; ActiveRecord::Migration[5.1]
    def change
      create_table :matches do |t|
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p>match_teams migration:</p>
  
  <pre><code>class CreateMatchTeams &lt; ActiveRecord::Migration[5.1]
    def change
      create_table :match_teams do |t|
          t.belongs_to :match, index: true, foreign_key: true
          t.belongs_to :team, index: true, foreign_key: true
          t.boolean "is_visitor", default: false
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p>So, I can do something like:</p>
  
  <pre><code>#create team here
  #create team2 here
  
  match = Match.new()
  match.teams = [team, team2]
  match.save!
  
  match_team_associative = match.match_team
  match_team_associative.each do |item|
    if item.team_id == team.id
      item.is_visitor = true
      item.save!
    end
  end
  </code></pre>
  
  <p>I don't know if this is the better solution, or if it's ok the manner I'm changing the value of is_visitor field in the associative table.</p>
  
  <p>Another soluction I think would be has two relations one to many between these entitties team and match. A table matches would have a foreign key for teams called team, and another foreign key for teams called visitor_team, like:</p>
  
  <p>matches table:</p>
  
  <pre><code>id | team | visitor_team
  1  |    1 | 2
  </code></pre>
  
  <p>But this second soluction I could't implement, I don't know how to do that. I find something <a href="https://stackoverflow.com/questions/30468368/2-or-more-one-to-many-relationships-between-two-tables-in-rails">here</a> but I could't make it work. Does anyone can help me telling me how to make it work that way?</p>
  
  <p>Which of these two solution is better? I think maybe the second, but it couldn't make it work.</p>
  
  <p>I appreciate any help.</p>
  
  <p>Thank you.</p>