I was trying to figure out if ActiveRecord caches model instances within a transaction (or even within a request) so I ran the following code and was surprised what I saw:
User.transaction do
u1 = User.find(1)
u2 = User.find(1)
puts u1.eql?(u2) # outputs true, this means they are the same instance
puts u1.email # outputs 'user@gmail.com'
u1.email = 'foo' # change the email, should change on u1 and u2
puts u1.email # outputs 'foo'
puts u2.email # outputs 'user@gmail.com' # how can the values be different if u1 and u2 are the same instance?
end
Isn't .eql? in Ruby supposed to evaluate instance equality? What gives? Does ActiveRecord override this method? How can my two references point to the same instance and have different values?