I have a Team object and a Game object.
A Game is supposed to have one winner which is a Team
A Team can be the winner of multiple Games
How can I structure this correctly? I’m using Mongoid
This is what I came up with so far…
class Game
include Mongoid::Document
include Mongoid::Timestamps
has_one :winner, :class_name=>Team
end
class Team
include Mongoid::Document
include Mongoid::Timestamps
has_and_belongs_to_many :games_won, :class_name=>"Game", :inverse_of => :Game
end
Consider abstracting the win out into its own class, so that:
This makes the structure more logical, makes the code simpler, and has other advantages for situations where you might want to start working with wins as a separate resource for other reasons.