avram
avram copied to clipboard
Checking `new_record?` when using upsert
I believe that new_record? will always be true when using upsert because new_record? basically does id.value.nil?. The id.value is only filled in when you pass in @record which doesn't happen on upserts.
I'll have to look more in to this, but we had an issue using new_record? with the upsert, and it seemed that it was always returning true no matter what.
Uhm, I'm not really sure I understood the problem, but adding the last line in the tests (for an upsert->update operation) passes
UpsertUserOperation.upsert(
name: "Rich",
nickname: nil,
age: 30,
joined_at: joined_at
) do |operation, user|
operation.created?.should be_false
operation.updated?.should be_true
operation.new_record?.should be_false #=> passes
Since I had to put the hands on it quite recently, upsert works this way.
The id is not filled initially if other columns are used for the lookup (in this case, name and nickname)
A query is performed to get the record that matches it - and then if the record is present - it is moved into the @record variable.
The only possible case in which I can see that being prefilled, is when the lookup columns coincide with the primary key ones (well, you really should directly use #save in that case, imho) <- needs more investigation