Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dirty (changed) attributes: since when are the values in the changeset strings (instead of objects)?

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?

like image 944
Joshua Muheim Avatar asked Aug 06 '19 14:08

Joshua Muheim


1 Answers

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

like image 145
Joshua Muheim Avatar answered Nov 11 '22 07:11

Joshua Muheim