The above should be pretty self explanatory (gosh rails is great!), but to make sure: I have players playing games as members of a team, and I am keeping statistics for those players.Code:# players class Player < ActiveRecord::Base has_many :rosters has_many :teams, :through => :rosters end # teams class Team < ActiveRecord::Base has_many :rosters has_many :players, :through => :rosters end # the relationship between players and teams class Roster < ActiveRecord::Base belongs_to :player belongs_to :team end # a line item account of a players performance in a game class Stat < ActiveRecord::Base belongs_to :player belongs_to :game end # games class Game < ActiveRecord::Base belongs_to :hometeam, :class_name => 'Team', :foreign_key => 'hometeam_id' belongs_to :awayteam, :class_name => 'Team', :foreign_key => 'awayteam_id' has_many :stats has_many :players, :through => :stats end
I have two issues:
1) I want to also add support for games where players play eachother one on one, but want these to exist as game objects because they ... well... are. An "is_teamgame?" with potentially null "player1_id" and "player2_id" seems totally unelegant. Is there a better way?
2) I want to add support for other games with different kinds of stats, yet also store those as "games". My current stat model corresponds to a table that just has the kind of statistic as a column, and the rows connect a player and the game he was playing in with all of those stats. Because I account for things like averages, it doesn't seem like I can just populate the table with more columns for more stats and null them out depending on the game type, since that would ruin my ability to, for instance, count how many games of a particular type a player has been in. How can I account for this?
I prefer simple, elegant solutions. It seems like this is the right place for some sort of table inheritance, but I'm not really sure how that works. Are there any resources on this info?





I like the ideas you are using.

Bookmarks