# Syncronization in rails

**URL:** <https://www.ruby-forum.com/t/syncronization-in-rails/167142>\
**Category:** Rails\
**Created:** [May 4, 2009, 4:27pm UTC](https://www.ruby-forum.com/t/syncronization-in-rails/167142 "2009-05-04T16:27:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![amit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@amit](https://www.ruby-forum.com/u/amit)\
**Post date:** [May 4, 2009, 4:27pm UTC](https://www.ruby-forum.com/t/syncronization-in-rails/167142/1 "2009-05-04T16:27:28Z")

</div>

Hello,

Here is the logic that I am trying to achieve. I have a User Model and  
the business rule is that user can accept maximum 5 friends requests.

for this I have an accept method in User Model which looks similar to  
below code snippet

def accept(friend)  
raise ArgumentError if self.friends.count \>= MAX\_FRIENDS\_ALLOWED  
…  
…  
#code to add friend to friends list  
end  
As you can see that I basically query for count of friends for a user  
and if the count is greater then or equal to MAX\_FRIENDS\_ALLOWED the  
friend accept request should not go through.

From the UI perspective I have a link\_to\_remote, “Accept” link that  
calls an action which calls the User Model’s “accept” method. Basically  
UI looks like

## FRIEND NAME | Action

AAAAA | Accept  
BBBBB | Accept  
CCCCC | Accept  
DDDDD | Accept  
EEEEE | Accept  
FFFFF | Accept

Now from UI I try to accept multiple friends requests by clicking on  
“Accept” link (which makes an ajax request) quickly.

When I run this in under normal development environment (running single  
mongrel), everything works fine i.e. the validation put in User Model’s  
“accept” method works.

After that I configured 4 mongrel in a cluster and ran it behind  
lighttpd and then ran in to trouble.

I guess the problem here is that since I run cluster of mongrels each  
mongrel executes the request in separate process because of which before  
the a friend is accepted, another process executes “self.friends.count”  
and it will not reflect the updated count. I tried to enclose the above  
statements in Activerecord transaction, but it did not work and the  
reason I think is that transaction is per-database connection and each  
process will spawn a separate connection.

Please suggest any workarounds or probably solution to this issue.

---

<div class="post-metadata">

**Author:** ![amit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@amit](https://www.ruby-forum.com/u/amit)\
**Post date:** [May 6, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/syncronization-in-rails/167142/2 "2009-05-06T17:52:23Z")

</div>

> **[ActiveRecord::Locking::Pessimistic](https://api.rubyonrails.org/classes/ActiveRecord/Locking/Pessimistic.html)**
>
> Locking::Pessimistic provides support for row-level locking using SELECT … FOR UPDATE and other lock types.

On May 4, 7:27 am, Amit R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
