Rails并没有保存已更改的属性[duplicate]

时间:2022-11-27 07:44:35

This question already has an answer here:

这个问题已经有了答案:

I am appending some text to a notes field on one of my ActiveRecord::Base models but when I save it, it doesn't get updated:

我在我的一个ActiveRecord: Base model上添加了一些文本到notes字段,但是当我保存它时,它不会更新:

valve.notes                          
#=> "Level: Top"

valve.notes << "\nDirection: North"

valve.notes                          
#=> "Level: Top\nDirection: North"

valve.save                           
#=> true

valve.reload.notes                   
#=> "Level: Top"

1 个解决方案

#1


8  

Concat doesn't tell ActiveRecord that an Attribute has changed.

Figured it out and wanted to share it here for others (and most likely myself!) in the future.

找到了答案,并想在这里分享给其他人(很可能是我自己!)

I didn't know this but ActiveRecord cannot determine that an attribute has been changed (i.e. is dirty) when you concatenate it, either with concat() or <<. And because ActiveRecord only saves, or updates, attributes that have changed (i.e. are dirty), it doesn't update that attribute.

我不知道这一点,但是ActiveRecord无法确定当您将它与concat()或<<.连接在一起时,属性已经被更改(即脏)。而且因为ActiveRecord只保存或更新那些已经更改的属性(即脏的),它不会更新该属性。

It's a sneaky little gotcha if you're not already aware of it because it not only fails silently, it doesn't think it's failed at all (and perhaps it hasn't, if you ask the ActiveRecord authors :).

如果你还没有意识到这一点,这是一个狡猾的小陷阱,因为它不仅无声地失败,而且它根本不认为它失败了(如果你问ActiveRecord作者的话,它可能还没有失败)。

valve.notes            
#=> "Level: Top"

valve.notes << "\nDirection: North"

valve.changed?         
#=> false

valve.notes_changed?   
#=> false

valve.save
#=> true

valve.reload.notes
#=> "Level: Top"

You can read more about this on Rails' API Docs.

您可以在Rails的API文档中了解更多。

Solution

To get around this you need to do one of two things:

要解决这个问题,你需要做以下两件事中的一件:

  1. Let ActiveRecord know that the notes attribute has changed (i.e. it is now dirty):

    让ActiveRecord知道notes属性已经更改(即现在是dirty):

    valve.notes << "\nDirection: North"
    
    valve.changed?                      
    #=> false
    
    valve.notes_will_change!
    
    valve.changed?                      
    #=> true
    
  2. Don't use concat() or << to append to your attributes:

    不要使用concat()或<< < <来附加属性:< p>

    valve.notes = "#{valve.notes}\nDirection: North"
    
    valve.changed?                      
    #=> true
    

Hope that helps at least one other soul.

希望这至少能帮助另一个灵魂。

JP

摩根大通

#1


8  

Concat doesn't tell ActiveRecord that an Attribute has changed.

Figured it out and wanted to share it here for others (and most likely myself!) in the future.

找到了答案,并想在这里分享给其他人(很可能是我自己!)

I didn't know this but ActiveRecord cannot determine that an attribute has been changed (i.e. is dirty) when you concatenate it, either with concat() or <<. And because ActiveRecord only saves, or updates, attributes that have changed (i.e. are dirty), it doesn't update that attribute.

我不知道这一点,但是ActiveRecord无法确定当您将它与concat()或<<.连接在一起时,属性已经被更改(即脏)。而且因为ActiveRecord只保存或更新那些已经更改的属性(即脏的),它不会更新该属性。

It's a sneaky little gotcha if you're not already aware of it because it not only fails silently, it doesn't think it's failed at all (and perhaps it hasn't, if you ask the ActiveRecord authors :).

如果你还没有意识到这一点,这是一个狡猾的小陷阱,因为它不仅无声地失败,而且它根本不认为它失败了(如果你问ActiveRecord作者的话,它可能还没有失败)。

valve.notes            
#=> "Level: Top"

valve.notes << "\nDirection: North"

valve.changed?         
#=> false

valve.notes_changed?   
#=> false

valve.save
#=> true

valve.reload.notes
#=> "Level: Top"

You can read more about this on Rails' API Docs.

您可以在Rails的API文档中了解更多。

Solution

To get around this you need to do one of two things:

要解决这个问题,你需要做以下两件事中的一件:

  1. Let ActiveRecord know that the notes attribute has changed (i.e. it is now dirty):

    让ActiveRecord知道notes属性已经更改(即现在是dirty):

    valve.notes << "\nDirection: North"
    
    valve.changed?                      
    #=> false
    
    valve.notes_will_change!
    
    valve.changed?                      
    #=> true
    
  2. Don't use concat() or << to append to your attributes:

    不要使用concat()或<< < <来附加属性:< p>

    valve.notes = "#{valve.notes}\nDirection: North"
    
    valve.changed?                      
    #=> true
    

Hope that helps at least one other soul.

希望这至少能帮助另一个灵魂。

JP

摩根大通