hyperstack-org / hyperstack-org/hyperstack

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

Ouverte
#232 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
JavaScript
Étoiles
538
Forks
41
Métriques de merge des PR
Aucune PR mergée en 30 j

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).
```

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.