Forum: Ruby on Rails Syncronization in rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
096784b51d6fba9d5583654bf9526da1?d=identicon&s=25 Amit Rawal (amit141)
on 2009-05-04 16:27
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.
Fb4a8343342eb941470aa99eee399578?d=identicon&s=25 bulletgani (Guest)
on 2009-05-06 17:52
(Received via mailing list)
http://api.rubyonrails.org/classes/ActiveRecord/Lo...

On May 4, 7:27 am, Amit Rawal <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.