active_record_upsert
active_record_upsert copied to clipboard
Specifying upsert attributes for associated data
I am trying to create/ update a record with associated data and also with a specific field only for the the create operation. I am running into the following situation:
params = { name: 'a', nested_resource: { name: 'b' } }
record = MyRecord.new(params)
record.special_attribute = 'create'
record.upsert
In this case the nested_resource
gets updated if the record gets updated, but the special_attribute
gets set as well.
If I want to keep the special_attribute
only on create and switch to something like this:
record = MyRecord.new(params)
record.special_attribute = 'this'
record.upsert(attributes: params.keys)
Then the nested resource doesn't get updated when the record gets updated (a new associated record is created instead).
Is there a way to specify nested attributes just for update operation?
I triedrecord.update(params)
and it doesn't work for me.