Meine Modelle:Accepts_nested_attributes_for wirft Active :: InvalidForeignKey: PG :: ForeignKeyViolation: ERROR
class Foo < ActiveRecord::Base
has_many :bars, inverse_of: :foo
accepts_nested_attributes_for :bars
...
end
class Bar < ActiveRecord::Base
belongs_to :foo, inverse_of: :bars
...
end
Wenn ich versuche, Aufzeichnungen zu erstellen, wie so:
Foo.create(foo_attribute: value, bars_attirbutes: [{bar_attribute: value}])
ich:
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "bars" violates foreign key constraint "bars_foo_id_fkey"
DETAIL: Key (foo_id)=(14) is not present in table "foos".
Also ich denke, ActiveRecord versucht, das verschachtelte Modell zu speichern, bevor das übergeordnete Modell gespeichert wird und damit die Fehler r. Aber warum macht es das? Wie kann ich das verhindern?