Forum: Ruby on Rails Syncronization in rails

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Amit R. (Guest)
on 2009-05-04 18:27

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
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

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.
bulletgani (Guest)
on 2009-05-06 19:52
(Received via mailing list)

On May 4, 7:27 am, Amit R. <removed_email_address@domain.invalid>
This topic is locked and can not be replied to.