ActiveRecord :: Base.connection.execute之后ActiveRecord是否自动更新?

时间:2014-12-10 08:21:06

标签: ruby-on-rails activerecord

ActiveRecord::Base.connection.execute('UPDATE ...')被解雇或需要重新加载后,ActiveRecord模型是否自行更新?

1 个答案:

答案 0 :(得分:1)

不,每次你直接对连接做一些事情,你都是独立的,需要确保一切都是同步的。

c = Car.create! name: "Car 1"
c.name # => "Car 1"

Car.connection.execute "UPDATE cars SET name = 'Car 2' WHERE id = 1"

c.name # => "Car 1"
c.reload.name # => "Car 2"