What i have: (Action in Controller)
def create
@test = Test.new(params[:test])
@test.save
devicefiles = params[:devicefiles]
if devicefiles != nil
devicefiles.each do |attrs|
devicenote = Testdevicenote.new(attrs, :test_id => @test.id)
devicenote.save
end
end
end
This controller action does not show any error message and is rendering the view, but :test_id is not being saved in the database. How can i solve this?
EDIT: Ok whoops, I see it now…
Models only take one hash on initialize, not 2.
In short no one here has any clue, because that’s not enough information. We dont know how your models are setup.
But when debugging models that “won’t save” it’s often good to use the bang version
save,save!.savereturnstrueorfalseletting you know if it was able to save the record. Butsave!will raise exceptions when the model can’t be saved, and the exception will tell you why.That exception will likely tell you why the record is not being saved.
Also, its usually better to use the associations, rather than manage the ids yourself.