我有一个Team对象和一个Game对象。
Game应该有一个获胜者,即Team Team可以是多个Games的获胜者
我怎样才能正确地组织它呢?我用的是Mongoid
这就是我到目前为止想到的.
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发布于 2012-11-26 04:45:42
考虑将win抽象到它自己的类中,这样:
class Game
has_one :win
end
class Team
has_many :wins
end
class Win
belongs_to :game
belongs_to :team
end这使结构更符合逻辑,使代码更简单,并且对于出于其他原因而希望将wins作为单独资源开始使用的情况,还具有其他优点。
https://stackoverflow.com/questions/13555111
复制相似问题