三个 models :Game,Player,Hint 每个 Game 有若干 Player,同时有若干 Hint, 每个 Player 可以又一个 Hint 或者没有
我现在是这样写的
class Game < ApplicationRecord
has_many :players, dependent: :destroy, inverse_of: :game
accepts_nested_attributes_for :players
has_many :hints, through: :players, dependent: :destroy
accepts_nested_attributes_for :hints
class Player < ApplicationRecord
belongs_to :game, inverse_of: :players
has_one :hint
class Hint < ApplicationRecord
belongs_to :player
end
但是这样有个问题
@game.hints.create!(Lv:1,content:"房间#{i}不是出口")
的时候,会报错:
Validation failed: Player must exist
因为每个 Hint 可以属于某个 Player 也可以不属于,所以我又不能用:
player.hints.create
正确应该如何表述它们的关系呢?