Rails:update_attributes不会更新引用同一模型实例的任何其他现有实例

Kar*_*ran 3 attributes ruby-on-rails update-attributes

我意识到以下几点:

  user1 = User.create - creates a model with id 1 and age  is 0

  same_user = User.find(1)

  same_user.update_attributes(:age => 18)

  p same_user.age # prints out 18
  p user1.age # prints out 0
Run Code Online (Sandbox Code Playgroud)

为什么现有属性不从数据库中获取值?

fl0*_*00r 6

您需要重新加载您的用户.因为它的属性在数据库中更改时保存在内存中.

p same_user.age
p user1.reload.age
Run Code Online (Sandbox Code Playgroud)