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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:48:59+00:00 2026-06-08T16:48:59+00:00

first post on SO so I apologise if I’m not following the guidelines correctly.

  • 0

first post on SO so I apologise if I’m not following the guidelines correctly.

I am designing an online league management application in Rails and have come across some issues in the database design which I’d rather sort out now than go ahead with what I’ve got and find out it’s not going to work later.

Currently I have the following models:

class League < ActiveRecord::Base
    has_many :teams
    has_many :players
end

class Team < ActiveRecord::Base
    belongs_to :league
    has_many :players
end

class Player < ActiveRecord::Base
    belongs_to :league
    belongs_to :team
end

The idea is that league can either be made up of teams of players, or just individual players without a team. I have currently have a league_type attribute in the Leagues table which identifies whether the league is for teams or players. My first question is whether or not this is a good way to handle this situation?

One of the reasons this seems like it may be a problem is when I try to create a match, because it will either be made up of players or teams. I originally thought I’d have a Match model with home_id and away_id attributes, and then populate those with either player_ids or team_ids depending on the league type, however that doesn’t feel like good design to me. I’ve considered making the home and away fields polymorphic, perhaps that’s the best way? Eg.

class Match < ActiveRecord::Base
    belongs_to :home_matchable, polymoprhic: true
    belongs_to :away_matchable, polymoprhic: true
end

And then adding “has_many :home_matches, as: home_matchable” and “has_many :away_matches, as: away_matchable” to Teams and Players.

I’m finding just with the limited number of views and controllers I have at the moment, I seem to be needing to regularly check whether the league is for teams or players, so there’s a lot of “if league_type == ‘teams’ do something else do something else” statements in my code which feels a bit ugly. Especially if down the track I decide I want to add a “doubles” type for example, so they would turn into “if ‘teams’ then else if ‘players’ then else” etc.

Sorry this turned out a lot longer than I expected, any help is much 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-08T16:49:00+00:00Added an answer on June 8, 2026 at 4:49 pm

    Why not force a player to be in a team by default?
    When creating a player in DB, you’d automatically create a team for him and make him the sole member.

    You could also mark these teams with a boolean telling you these are not multi-player teams.

    This way, you’d only deal with teams when creating Matches. I personally don’t like the polymorphic approach, it can become tricky to manipulate different Models while accessing your associations.

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

Sidebar

Related Questions

My first post here :) I have a summer job doing a bit of
This is my first post and I my first experience with jquery. I have
This is my first post to I apoligise if its not perfectly put. Essentially
first post, so play nice! I have a fairly basic question about Python dictionaries.
First, appologies for the long post, I have tried to cut it down as
First post on stackoverflow, apologies beforehand for anything noob-like. I have been receiving an
I must admit, this is my first post on this site, so I apologise
the Solution is at the bottom of my post. First off, let me apologise
This is my first post on stack overflow, so I apologize if I have
First, a thank you in advance. Second, this is my first post so apologies

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.