Rails: update_attributes does not update any other existing instances referring to the same model instance

StackOverflow https://stackoverflow.com/questions/10463666

문제

I realised the following:

  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

Why doesnt the existing attribute obtain values from the database?

도움이 되었습니까?

해결책

You need to reload your user. Because it's attributes are saved in memory while they are changed in the databse.

p same_user.age
p user1.reload.age
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top