Ruby on Rails Triggers Update on Serialized Attribute Every Time

前端 未结 1 692
谎友^
谎友^ 2021-02-10 21:24

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_         


        
相关标签:
1条回答
  • 2021-02-10 22:09

    This is expected behavior. It is very difficult to detect changes within a serialized attribute, so they are updated on 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
    
    0 讨论(0)
提交回复
热议问题