# Active\_record\_context, optimistic locking and object reload

**URL:** <https://www.ruby-forum.com/t/active-record-context-optimistic-locking-and-object-reload/115594>\
**Category:** Rails\
**Created:** [September 26, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/active-record-context-optimistic-locking-and-object-reload/115594 "2007-09-26T18:00:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [September 26, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/active-record-context-optimistic-locking-and-object-reload/115594/1 "2007-09-26T18:00:54Z")

</div>

It seems that active\_record\_context (by Rick O.) and optimistic  
locking don’t play too well together.  
When I enable active\_record\_context for the entire design, objects  
don’t properly reload.

This is more or less what I’m doing:

while !ok {  
begin  
Transaction {  
object1.save!  
object2.save!  
ok = true  
}

rescue  
object1.reload  
object2.reload  
… reapply my changes to the objects …  
end  
}

So, when I’m using active\_record\_context, which does wonders to the  
database accesses, it seems that the reload doesn’t function  
correctly. I don’t see any database fetches and thus, optimistic  
locking keeps failing (since the other mongrel instances have changed  
the data).

active\_record\_context:  
[http://www.activereload.net/2007/5/23/spend-less-time-in-the-database-and-more-time-outdoors](http://www.activereload.net/2007/5/23/spend-less-time-in-the-database-and-more-time-outdoors)

Any ideas?

Thanks,  
Helzer

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [September 26, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/active-record-context-optimistic-locking-and-object-reload/115594/2 "2007-09-26T18:07:46Z")

</div>

active\_record\_context overrides find to return cached values, reload  
basically replaces the object’s attributes with the result of  
self.class.find(self.id) so this isn’t unexpected. It would be hard for  
active\_record\_context to also override reload so that it removes the  
current object from the cache.

Fred

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [September 26, 2007, 6:16pm UTC](https://www.ruby-forum.com/t/active-record-context-optimistic-locking-and-object-reload/115594/3 "2007-09-26T18:16:07Z")

</div>

Thanks Fred,

Is there a way for me to force reload from the database?  
I just need it in a very limited number of places, so doing it  
manually will not be a problem.

Helzer

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [September 26, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/active-record-context-optimistic-locking-and-object-reload/115594/4 "2007-09-26T18:23:54Z")

</div>

helzer wrote:

> Thanks Fred,
> 
> Is there a way for me to force reload from the database?  
> I just need it in a very limited number of places, so doing it  
> manually will not be a problem.

Not without overriding reload: reload just calls find, which ar\_context  
changes: you need to add code to reload that dumps the cache (and the  
nice thing to do after that would be to give the changes back to rick)

Fred

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [September 26, 2007, 7:52pm UTC](https://www.ruby-forum.com/t/active-record-context-optimistic-locking-and-object-reload/115594/5 "2007-09-26T19:52:24Z")

</div>

On 9/26/07, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> nice thing to do after that would be to give the changes back to rick)  
> Oh, I think I may have run into this without realizing it. I just  
> shuffled my callbacks around a bit and it worked, but I didn’t realize  
> this plugin was causing the issue. Um, gmail isn’t showing the rest  
> of this conversation, so just send me a proper bug report at  
> [Overview - Plugins - ar-code](http://ar-code.lighthouseapp.com/projects/3108-plugins/overview) or via  
> email. A patch would be lovely too of course.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [September 27, 2007, 3:04pm UTC](https://www.ruby-forum.com/t/active-record-context-optimistic-locking-and-object-reload/115594/6 "2007-09-27T15:04:03Z")

</div>

Hi Rick,

Funny thing, gmail stopped notifying me about thread additions too…  
I just created the ticket in lighthouse about this.  
[http://ar-code.lighthouseapp.com/projects/3108-plugins/tickets/5-active\_record\_context-prevents-object-reload-from-database](http://ar-code.lighthouseapp.com/projects/3108-plugins/tickets/5-active_record_context-prevents-object-reload-from-database)

Thanks,  
Amir
