这是 game 的 model
class Game < ApplicationRecord
has_many :players, dependent: :destroy
end
这是 player 的 model
lass Player < ApplicationRecord
belongs_to :game
validates :number, presence: true, uniqueness: true
end
我想一次性创建一个 game 和他的 players,这是 controller:
def test
@game = Game.new(game_params)
if @game.save
(0...5).each do |i|
@player = @game.players.create(number:i)
puts i
end
redirect_to root_path
end
end
但是会报错
unknown attribute 'game_id' for Player.