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 8329895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:56:46+00:00 2026-06-09T01:56:46+00:00

Very new to Rails, have managed a few simple projects, but now stepping into

  • 0

Very new to Rails, have managed a few simple projects, but now stepping into more complex associations between tables and was hoping for some help.

The scenario can best be related to a sports match. Let’s say we have

1) A Team (has_many players)

2) A Player (belongs_to team)

3) A Match — now it gets tricky.

A Match will have: 2 teams, and 22 players (11 on each side) that take part in it. Also, associated with each player, will be their scores for the match (for example, Shots on goal, Goals scored, Points, etc.)

What would be the best practice to create this kind of association? Any tips would be greatly appreciated.

  • 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-06-09T01:56:48+00:00Added an answer on June 9, 2026 at 1:56 am

    Models

    app/models/team.rb

    class Team < ActiveRecord::Base
        has_many :players, inverse_of: :team
        has_many :team_matches
        has_many :matches, through: :team_matches
    end
    

    app/models/player.rb

    class Player < ActiveRecord::Base
        belongs_to :team, inverse_of: :player
        has_many :player_matches
        has_many :matches, through: :player_matches
    end
    

    app/models/match.rb

    class Match < ActiveRecord::Base
        has_many :players, through: :player_matches
        has_many :teams, through: :team_matches
    end
    

    app/models/team_match.rb

    class TeamMatch < ActiveRecord::Base
        belongs_to :team
        belongs_to :match
    end
    

    app/models/player_match.rb

    class PlayerMatch < ActiveRecord::Base
        belongs_to :player
        belongs_to :match
    end
    

    Migrations

    db/migrate/create_matches.rb

    class CreateMatches < ActiveRecord::Migration
      def change
        create_table :matches do |t|
          t.datetime :happened_at
          t.timestamps
        end
      end
    end
    

    db/migrate/create_players.rb

    class CreatePlayers < ActiveRecord::Migration
      def change
        create_table :players do |t|
          t.string :name
          t.timestamps
        end
      end
    end
    

    db/migrate/create_teams.rb

    class CreateTeams < ActiveRecord::Migration
      def change
        create_table :teams do |t|
          t.string :name
          t.timestamps
        end
      end
    end
    

    db/migrate/create_player_matches.rb

    class CreatePlayerMatches < ActiveRecord::Migration
      def change
        create_table :player_matches do |t|
          t.integer :match_id
          t.integer :player_id
          t.integer :player_shots_on_goal
          t.integer :player_goals_scored
          t.timestamps
        end
      end
    end
    

    db/migrate/create_team_matches.rb

    class CreateTeamMatches < ActiveRecord::Migration
      def change
        create_table :team_matches do |t|
          t.integer :match_id
          t.integer :team_id
          t.integer :team_points
          t.timestamps
        end
      end
    end
    

    Edit1: @Mischa should share credit here! 🙂

    Edit2: Sorry about the many versions, I totally underestimated this problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a very simple blog application using Ruby on Rails. New to
So I'm very new to working in SQL in general, let alone rails but
This is probably a very simple question, I apologise - I'm new to rails.
I am new to Rails, and I was trying to deploy a very simple
I am very new to rails, and from what i have been reading and
I am very, very new to Rails development. I have read the arguments against
I am very new to rails development. I am creating a simple backend for
Most rails projects have a very specific version included in their gemfile, right after
I'm very new to Rails, so it's likely there's a simple concept here that
I'm very new to Ruby on Rails, and have been getting the following error

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.