# Selective optimistic locking

**URL:** <https://www.ruby-forum.com/t/selective-optimistic-locking/90041>\
**Category:** Rails\
**Created:** [March 25, 2007, 1:06pm UTC](https://www.ruby-forum.com/t/selective-optimistic-locking/90041 "2007-03-25T13:06:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cinek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@cinek](https://www.ruby-forum.com/u/cinek)\
**Post date:** [March 25, 2007, 1:06pm UTC](https://www.ruby-forum.com/t/selective-optimistic-locking/90041/1 "2007-03-25T13:06:48Z")

</div>

I’m working on a photo gallery and use optimistic locking to prevent  
concurrent editing of photo description.  
At the same time I want to gather view statistics, incrementing a  
counter every time a user views a larger version of a photo.

There are two solutions that came to my mind:

- split photo into two models: description and statistics,
- update view count without increasing lock\_version.

The first one is a bit troublesome (I’d need to maintain a real 1 : 1  
relationship, not a 1 : 0 … 1), so I decided to implement the second.

My problem is that there’s no easy and _documented_ way to do this.  
Here’s my solution:

(in the Photo model)  
def increment\_view\_count!  
Photo.transaction do  
lock!  
# optimistic lock must by turned off for this update  
increment(:views)  
update\_without\_lock  
end  
end

update\_without\_lock is defined in ActiveRecord::Locking::Optimistic,  
it’s an alias to original update from ActiveRecord::Base, afaik.

This allows a privileged user to edit a photo and others may increase  
the view count at the same time.

## Could you comment on this? Maybe you have some other ideas?

Marcin S.

---

<div class="post-metadata">

**Author:** ![cinek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@cinek](https://www.ruby-forum.com/u/cinek)\
**Post date:** [March 26, 2007, 2:46am UTC](https://www.ruby-forum.com/t/selective-optimistic-locking/90041/2 "2007-03-26T02:46:36Z")

</div>

Marcin S. wrote:

> def increment\_view\_count!  
> Photo.transaction do  
> lock!  
> # optimistic lock must by turned off for this update  
> increment(:views)  
> update\_without\_lock  
> end  
> end

If you don’t need to read the current view count when  
incrementing, an alternative is:

def increment\_view\_count!  
Photo.increment\_counter(:views, id)  
end

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![cinek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@cinek](https://www.ruby-forum.com/u/cinek)\
**Post date:** [March 26, 2007, 5:18pm UTC](https://www.ruby-forum.com/t/selective-optimistic-locking/90041/3 "2007-03-26T17:18:06Z")

</div>

Mark Reginald J. wrote:

> Marcin S. wrote:
> 
> > def increment\_view\_count!  
> > Photo.transaction do  
> > lock!  
> > # optimistic lock must by turned off for this update  
> > increment(:views)  
> > update\_without\_lock  
> > end  
> > end
> 
> If you don’t need to read the current view count when  
> incrementing, an alternative is:
> 
> def increment\_view\_count!  
> Photo.increment\_counter(:views, id)  
> end

Good point, thanks.

–  
Marcin S.
