I am planning a relational database to store poker game data (like what would be included in the hand histories). I would like help figuring out how to design the associations. It seems like there should be 4 models: Game, Hand, Player, and Action (single action of a given player, like raise, fold, call). Let me lay out what I have:
class Game < ActiveRecord::Base
has_many :hands
has_many :actions
has_and_belongs_to_many :players
end
class Hand < ActiveRecord::Base
has_many :actions
belongs_to :game
has_and_belongs_to_many :players
end
class Action < ActiveRecord::Base
belongs_to :game
belongs_to :hand
belongs_to :player
end
class Player < ActiveRecord::Base
has_and_belongs_to_many :games
has_and_belongs_to_many :hands
has_many :actions
end
Does this make sense?
If you’re planning to use
has_and_belongs_to_many, you should probably switch to usinghas_many ..., :throughas it’s much easier to manage. You already have an Action model that does what you need without having to create some join tables:Generally, the fewer tables you have involved in your queries, the faster they will run. Involving any kind of
JOINis going to lead to unpredictable query performance.Be sure to index your tables carefully, and use the
EXAMINEstatement to ensure you’re hitting indexes when using them. Table scans will be extremely painful if you load this up with millions of records, and that doesn’t take long in games like this as a single hand involves dozens of actions and it’s typical to play dozens of hands every hour.