ActiveRecord 3.2 bug: Can not save when mutate string attribute
This is the English version of my original post.
Note: This bug is fixed from ActiveRecord 4.0
The weird bug
The cause
When you use save
, Rails check to see whether the attributes are changed
before update the record in database. To optimize that process, it used
the reference instead of the value of the attributes. Therefore, when you
mutate the attribute (using gsub!
or something similar), the reference
won’t change, and it won’t pass the check.
How to fix
Simply create another reference for the attribute:
Lession learn
Mutation lead to a lot of side-effect bugs like this, so I try to avoid it whenever possible. It’s just so convenient in Rails, though…