Paul
Paul

Reputation: 26670

Does ActiveRecord update itself after ActiveRecord::Base.connection.execute?

Does ActiveRecord model update itself after ActiveRecord::Base.connection.execute('UPDATE ...') was fired or it needs to be reloaded?

Upvotes: 0

Views: 174

Answers (1)

Jiří Pospíšil
Jiří Pospíšil

Reputation: 14412

No, every time you do something with the connection directly, you are on your own and need to make sure everything is in sync.

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"

Upvotes: 1

Related Questions