We ran into an interesting problem today. It seems that if you use the shovel operator to concatenate a string attribute on an ActiveRecord model, it doesn't make the model dirty. For example:
e = Employee.first
e.name << "asdf"
e.name_changed? # returns false
e.changed? # returns false
This makes sense since the shovel operator updates a string without making a copy of it, where the += operator will make a copy of the string. I don't see how ActiveRecord could possibly know that something changed if you use the shovel operator.
Has anyone else seen this? Is the solution to just use +=
instead of <<
when concatenating strings?
The solution is was you write.
Or you can mark before that your attibute will_change
e = Employee.first
e.name_will_change!
e.name << "asdf"
e.name_changed? # => true
It's mark on the API documentation. ActiveModel::Dirty
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