hyperstack-org / hyperstack-org/hyperstack

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

未关闭
#232 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
JavaScript
星标
538
派生
41
PR 合并指标
30 天内没有已合并 PR

描述

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

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。