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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:34:59+00:00 2026-05-14T23:34:59+00:00

I have three models in a Rails application: Game represents an instance of a

  • 0

I have three models in a Rails application: Game represents an instance of a game being played. Player represents an instance of a participant in a game. User represents a registered person who can participate in games.

Each Game can have many Players, and each User can have many Players (a single person can participate in multiple games at once); but each Player is in precisely one Game, and represents precisely one User. Hence, my relationships are as follows at present.

class Game
  has_many :players
end

class User
  has_many :players
end

class Player
  belongs_to :game
  belongs_to :user
end

… where naturally the players table has game_id and user_id columns, but games and users have no foreign keys.

I would also like to represent the fact that each Game has many Users playing in it; and each User has many Games in which they are playing. How do I do this? Is it enough to add

class Game
  has_many :users, :through => :players
end

class User
  has_many :games, :through => :players
end
  • 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-14T23:34:59+00:00Added an answer on May 14, 2026 at 11:34 pm

    What you propose should be enough; Now, each game should have a users[] array, and each user has a games[] array.

    http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

    :has_many :through is basically an extension of the typical many-to-many model where you have an intermediate table with ids for each one of the related entities. However, in Rails, the :has_and_belongs_to_many relationship doesn’t allow for the intermediate table to be an entity in itself, in that it has no meaning other than serving as glue between the two related models.

    :has_many :through allows a real entity or model in the application (in your case, players) to act as “glue” between two others (games, users) but also allowing you to manipulate “players” as in this case it does contain important information.

    Hope this helps.

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

Sidebar

Related Questions

I have an application with three Models (Profile -> SubModel -> SubSubModel) chained together
In my Rails application I have these models Order has_many OrderItem OrderItem belongs_to Order
In rails application I have two models: Food and Drink . Both food and
In my rails application my models have a to_html method. This method is called
In my Rails application, I have two models, Articles and Projects, which are both
I have a three-level multi-nested form in Rails. The setup is like this: Projects
We are creating a rails application for an already existing database. We need to
I'm developing an application that needs to store lots of records in an organized
I can easily ascend the class hierarchy in Ruby: String.ancestors # [String, Enumerable, Comparable,

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.