Ruby on Rails 每次都触发序列化属性的更新 [英] Ruby on Rails Triggers Update on Serialized Attribute Every Time
问题描述
我有一个带有名称和设置的简单用户模型.每次保存用户 AREL 后,都会更新设置列.例如:
I have a simple user model with a name and settings. After every save of user AREL is performing an update to the settings column. For example:
user = User.find_by_name('kevin')
user.save
(0.3ms) UPDATE "users" SET "updated_at" = '2011-10-20 19:58:06.363541', "settings" = '--- {}' WHERE "users"."id" = 1
调用 save 时不会更新任何其他字段.这是预期的行为吗?为什么要执行更新?如果序列化内容没有改变,可以关闭不改变吗?我上传了用于创建最小案例的示例项目:
None of the other fields are updated when calling save. Is this expected behavior? Why is it performing the update? Can it be turned off to not change if the serialized contents haven't changed? I've uploaded the sample project used to create a minimum case:
http://cl.ly/0p0j3Z3Y0L1x1I1p3Z0g
推荐答案
这是预期的行为.检测序列化属性中的更改非常困难,因此每次保存时都会更新它们.
This is expected behavior. It is very difficult to detect changes within a serialized attribute, so they are updated on every save.
考虑以下 (ruby 1.8.7) irb 会话:
Consider the following (ruby 1.8.7) irb session:
ruby-1.8.7-p352 :001 > x = "--- \n:b: 2\n:a: 1\n"
=> "--- \n:b: 2\n:a: 1\n"
ruby-1.8.7-p352 :002 > y = "--- \n:a: 1\n:b: 2\n"
=> "--- \n:a: 1\n:b: 2\n"
ruby-1.8.7-p352 :003 > x == y
=> false
ruby-1.8.7-p352 :004 > YAML.load(x) == YAML.load(y)
=> true
这篇关于Ruby on Rails 每次都触发序列化属性的更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!