Closed adis-io closed 1 year ago
I didn't know how to reproduce without user interaction. I will appreciate if somebody will give a hint.
Try changing biding.pry
with user.inspect
Thank you, @rafaelfranca
So when I type user
it inspects object?
Yes
describe 'dirty tracking' do
it 'is not dirty when freshly initialized' do
model = described_class.new
model.inspect
expect(model.changed?).to be_falsey
end
end
Test case not passing by now for models which use typed_store.
I think model should not be marked as dirty when you inspect it.
What do you think, guys? @rafaelfranca @byroot
Just ran into a version of this where a freshly loaded model is reporting as changed.
class Dummy < ActiveRecord::Base
typed_store :fields, coder: ::ActiveRecord::TypedStore::IdentityCoder do |s|
s.string :email
end
end
d = Dummy.last
d.changed? # => true
before = d.attribute_in_database(:fields)
after = d.fields
before == after # => true
@gaffneyc am also running into a similar issue, though in my case, the ActiveRecord
instance is only marked as dirty once I access any typed_store
field.
I believe the problem stems from how ActiveModel::Attribute
works:
d.send(:mutations_from_database).send(:attributes)["email"].send(:original_value_for_database)
will return the serialized version of the field, which will always be different from the “apparent”, deserialized version of that field, which ActiveModel
recognizes as changed_in_place? == true
I’ll investigate if we can sidestep this somehow.
This behavior is conflicting with activerecord's locking. To extend @gaffneyc's example, if you try to run this:
d = Dummy.last
d.with_lock do
# Doesn't matter what's in here
end
then ActiveRecord will throw an error:
RuntimeError: Locking a record with unpersisted changes is not supported. Use `save` to persist the changes, or `reload` to discard them explicitly.
+1
+1
Steps to reproduce
json_typed_store_bug.rb
ruby json_typed_store_bug.rb
Expected behaviour
tests passes
Actual behaviour
tests fails