jesjos / active_record_upsert

Upsert for Rails 5 / Active Record 5
MIT License
207 stars 51 forks source link

Specifying upsert attributes for associated data #119

Open cristina-cowboy opened 2 years ago

cristina-cowboy commented 2 years ago

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.