Reputation: 176
In my Game
model, I have
class Game < ApplicationRecord
has_many :assignments
has_many :users, through: :assignments
accepts_nested_attributes_for :assignments
after_create :create_assignments
def create_assignments
3.times { Assignment.create!(game_id: id, user_id: 1) }
end
end
This works but I need the user_id
to be blank when it's created (it will be edited at a later time). It won't create assignments unless there is a user_id
with a value.
I've tried leaving it blank and omitting it, but that doesn't work. I think I need to put optional: true
somewhere but I'm pretty stumped on this one.
Upvotes: 0
Views: 30
Reputation: 176
Needed to add :optional => true
on Assignment Model
class Assignment < ApplicationRecord
belongs_to :game
belongs_to :user, :optional => true
end
Upvotes: 1