# ActiveRecord allows un-synced aliases?

**URL:** <https://www.ruby-forum.com/t/activerecord-allows-un-synced-aliases/77620>\
**Category:** Rails\
**Created:** [March 7, 2006, 5:33am UTC](https://www.ruby-forum.com/t/activerecord-allows-un-synced-aliases/77620 "2006-03-07T05:33:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anthony\_DeRobertis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@Anthony\_DeRobertis](https://www.ruby-forum.com/u/Anthony_DeRobertis)\
**Post date:** [March 7, 2006, 5:33am UTC](https://www.ruby-forum.com/t/activerecord-allows-un-synced-aliases/77620/1 "2006-03-07T05:33:33Z")

</div>

q1 = Question.find(1)  
q2 = Question.find(1)

Now, we find q1. **id**!= q2. **id**.

It appears the two aliases are not kept in sync:

q1.name =\> ‘old name’  
q2.name = ‘new name’  
q2.save!  
q1.name =\> ‘old name’  
q2.name =\> ‘new name’  
q1.reload  
q1.name =\> ‘new name’

Even worse, is this fun:

q1.destroy  
q2.name = ‘third name’  
q2.save!

That actually runs; ActiveRecord happily updates all 0 rows that match  
(let’s hope IDs can never be re-used, or that could have just updated  
the wrong row)

Finally,

q2.reload  
ActiveRecord::RecordNotFound: Couldn’t find Question with ID=1

Why does ActiveRecord return two objects both representing a single row  
in the database?

---

<div class="post-metadata">

**Author:** ![Anthony\_DeRobertis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@Anthony\_DeRobertis](https://www.ruby-forum.com/u/Anthony_DeRobertis)\
**Post date:** [March 7, 2006, 8:07am UTC](https://www.ruby-forum.com/t/activerecord-allows-un-synced-aliases/77620/2 "2006-03-07T08:07:39Z")

</div>

This is correct. Rails doesn’t use IdentityMap pattern (in terms of  
PoEAA). You should be aware of that when you develop with Rails.

Kent

---

<div class="post-metadata">

**Author:** ![Anthony\_DeRobertis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@Anthony\_DeRobertis](https://www.ruby-forum.com/u/Anthony_DeRobertis)\
**Post date:** [March 7, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/activerecord-allows-un-synced-aliases/77620/3 "2006-03-07T21:07:54Z")

</div>

Kent S. wrote:

> This is correct. Rails doesn’t use IdentityMap pattern (in terms of  
> PoEAA). You should be aware of that when you develop with Rails.

Is .save! working on a record that (no longer) exists at least a bug?  
Especially on databases that return the number of effected rows (at  
least Pg and MySQL), this should be really easy to detect.

---

<div class="post-metadata">

**Author:** ![Anthony\_DeRobertis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@Anthony\_DeRobertis](https://www.ruby-forum.com/u/Anthony_DeRobertis)\
**Post date:** [March 8, 2006, 12:47am UTC](https://www.ruby-forum.com/t/activerecord-allows-un-synced-aliases/77620/4 "2006-03-08T00:47:44Z")

</div>

That particular case I’d recommend to handle with database  
transactions and an appropriate isolation level set. Also you should  
look at the AR::Locking facility at

[http://api.rubyonrails.org/classes/ActiveRecord/Locking.html](http://api.rubyonrails.org/classes/ActiveRecord/Locking.html)

Kent.
