I'm using optimistic locking to prevent people overwriting each others changes in race conditions.
Since I upgraded Rails from 5.1
to 5.2
, my specs break, and I tracked it down to the fact that in the changes
array, the changes that are related to a file upload are not any longer Uploader
elements, but bare strings.
Before:
[1] pry(#<User>)> change
=> [
[0] #<AvatarUploader:0x007fcc7117bc00 # Value before
[1] #<AvatarUploader:17bc0cc7100x997f # Current value
Now:
[1] pry(#<User>)> change
=> [
[0] "image.jpg", # Value before
[1] "avatar.png" # Current value
]
How can I fix this?
I didn't find out why the afore-mentioned behaviour changed, but I could fix it.
Code before:
@user.changes.map do |attribute, change|
unless ['updated_at', 'lock_version'].include? attribute
StaleInfo.new resource: resource,
attribute: attribute,
value_before: change[0],
value_after: change[1]
end
end
Code now:
@user.changes.map do |attribute, change|
unless ['updated_at', 'lock_version'].include? attribute
StaleInfo.new resource: resource,
attribute: attribute,
value_before: resource.class.find(resource.id).send(attribute),
value_after: resource.send(attribute)
end
end
It feels quite quirky this way though, and it needs an additional DB query (to load the original object).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With