jesjos / jesjos/active_record_upsert

Updating does not fire callbacks with updated attributes

Open
#87 9 comments 0 reactions 0 assignees View on GitHub
Dominant language
Ruby
Stars
207
Forks
49
PR merge metrics
No merged PRs in 30d

Description

When updating a record via `upsert!`, callbacks do not use the latest attribute values. See example below:

```ruby
class Car < ActiveRecord::Base
upsert_keys [:internal_id]

before_save :calc_sum

def calc_sum
self.sum = first_val + second_val
end
end

Car.upsert!(internal_id: 1, first_val: 1, second_val: 2) # sets sum to 3

car = Car.upsert!(internal_id: 1, first_val: 2, second_val: 2) # *should* set sum to 4

car.sum == 4 # returns false, sum is still 3
```

I think its related to [this commit](https://github.com/jesjos/active_record_upsert/commit/6387036bf91397cd6203cc7c279d8c7b76020771).

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the upsert! implementation and inspect the commit linked in the issue, then reproduce the provided Car example. Done means the before_save callback sees the updated attributes and the second upsert produces sum == 4; run the existing test suite and add coverage where the current behavior is exercised.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.