Note that there are some explanatory texts on larger screens.

plurals
  1. POdatabase structure for league application
    primarykey
    data
    text
    <p>first post on SO so I apologise if I'm not following the guidelines correctly.</p> <p>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.</p> <p>Currently I have the following models:</p> <pre><code>class League &lt; ActiveRecord::Base has_many :teams has_many :players end class Team &lt; ActiveRecord::Base belongs_to :league has_many :players end class Player &lt; ActiveRecord::Base belongs_to :league belongs_to :team end </code></pre> <p>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? </p> <p>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.</p> <pre><code>class Match &lt; ActiveRecord::Base belongs_to :home_matchable, polymoprhic: true belongs_to :away_matchable, polymoprhic: true end </code></pre> <p>And then adding "has_many :home_matches, as: home_matchable" and "has_many :away_matches, as: away_matchable" to Teams and Players. </p> <p>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.</p> <p>Sorry this turned out a lot longer than I expected, any help is much appreciated! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload