我正在用Players,Frames和Throws写一个保龄球应用程序。玩家有框架和投掷。一个帧有多个投掷。并且投掷可以与多个框架相关联(例如,当在框架中投掷罢工时,后续投掷需要用于在罢工框架中进行评分)。
我的模型看起来像这样:
class Player < ActiveRecord::Base
has_many :frames
has_many :throws
end
class Frame < ActiveRecord::Base
belongs_to :player
has_and_belongs_to_many :throws
end
class Throw < ActiveRecord::Base
belongs_to :player
has_and_belongs_to_many :frames
end
让这三个都像这样相关是不好的设计?我应该考虑不同的架构吗?
答案 0 :(得分:0)
我认为您可以使用更简单的结构来满足您的要求。使用以下结构,您可以跟踪每个玩家的每个帧和帧的投掷。
class Player < ActiveRecord::Base
has_many :frames
end
class Frame < ActiveRecord::Base
belongs_to :player
has_many :throws
end
class Throw < ActiveRecord::Base
belongs_to :frame
end
或者如果您需要分别跟踪特定玩家的投掷和帧,并且还需要相互关联的框架和投掷的关联。您可以执行以下操作:
class Frame < ActiveRecord::Base
has_many :throws, through: :players
end
class Player < ActiveRecord::Base
belongs_to :frame
belongs_to :throw
end
class Throw < ActiveRecord::Base
has_many :frames, through: :players
end