hyperstack-org / hyperstack-org/hyperstack

ActiveRecord::Base#changed? does not pick up on deeper changes to serialized data

Open
#232 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
538
Forks
41
PR merge metrics
No merged PRs in 30d

Description

```ruby
class MyModel < ApplicationRecord
serialize :data
end

model = MyModel.create(data: {'Amsterdam' => [0, nil])

puts model.changed? # returns false as expected.
model.data['Amsterdam'][1] = 'Capital, not the seat of government'
puts model.changed? # returns false where true is expected.
```
Reason for this seem to be that the `ReactiveRecord::Setters#change_status_and_notify_helper` is not triggered when setting the `model.data['Amsterdam'][1]` value.
The current implementation seems to set a '!CHANGED!' value when changing values in a ActiveRecord attribute. The `ActiveRecord::Base#changed?` function checks for this string and does not compare the ActiveRecord attribute values. A possible solution might be to compare values in case of a serialized ActiveRecord attribute?

Workaround is to change the `model.data` attribute at the base of the structure:
```
puts model.changed? # returns false
model.data['Timmystan'] = [0, 'Place to be']
model.data.delete('Timmystan')
puts model.changed? # returns true where false is expected (as the added data is removed, returning the data structure to its original state).
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.