Using accepts_nested_attributes_for, we are also creating and updating related models at the same time.
There was a case where I wanted to update only the related model at the timing of creation.
https://github.com/rails/rails/issues/7256#issuecomment-93172189 It is a memo that I chewed for myself.
Foo (existing update)-> Bar (create)-> Baz (existing update) ・ Foo is updated ・ Create Bar ・ Baz updated I want to
In that case, just pass params as follows.
{
  foo: {
    id: "foo_id", =>Pass id to update foo
    ...
    bar_attributes: {
      ...
      baz_id: "baz_id", =>baz to update baz_id and
      baz_attributes: {
        id: "baz_id",  =>Pass the id.
        ...
      }
    }
}
        Recommended Posts