Ruby on Rails updates triggers by serial attribute every time

I have a simple user model with a name and settings. After each user save, AREL updates in the settings column. For instance:

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 

None of the other fields are updated when the save is called. Is this expected behavior? Why is he updating? Is it possible to disable it so as not to change if the serialized content has not changed? I uploaded a sample project that was used to create a minimal case:

http://cl.ly/0p0j3Z3Y0L1x1I1p3Z0g

+4
source share
1 answer

This is the expected behavior. It is very difficult to detect changes in a serialized attribute, so they are updated with every save.

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 
+8
source

All Articles