ruby on rails - Best way to set up model and database -
i creating app deals sports teams. have 2 models. users.rb model , teams.rb model. within users of app have team captains , teammates. give captain privledge of creating team when sign up. captain belong team , teammates belong team. team have 1 captain. best way set up? should have captain , teammate alias in users model? or improve create captains.rb model?
ok, first lets lay out relationship assumptions since there vague descriptions:
a user can:
be many captains be on many teamsa team can:
belong 1 captain have many users teammatesfrom think need 3 models:
user team teammates acts has_many_through bring together table between users , teamsthe teammate model have:
user_id team_id possible role/type fieldand leaves point have create decision app based on how utilize data. since team have 1 captain leave out role field in teammate model , add together user_id or captain_id actual team model. mean captain treated special , different other team mates always. however, i'm guessing somethings want loop through members of team including captain. that's why should create captain special role on teammate model , add together validation create sure there 1 captain. don't have special handling captain whenever looping through entire team.
assuming go role field final models have next relations:
teammate
belongs_to :team belongs_to :useruser
has_many :teammates has_many :teams, through: :teammatesteam
has_many :teammates has_many :users, through: :teammates has_one :team_captain, -> { role: 'captain' }, class_name: 'teammate'you fancier lastly 1 working out has_many through captain user instead of captain team_mate. post has interesting details on advanced has_one:
http://www.rojotek.com/blog/2014/05/16/cool-stuff-you-can-do-with-rails-has_one/
ruby-on-rails ruby database rails-activerecord models
No comments:
Post a Comment