Forum: Ruby on Rails Question on Data Concurrency

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.
srj (Guest)
on 2009-01-17 18:47
(Received via mailing list)
In Rails 2.2.2, if I retrieve a record using a Model.find method (any
of the finds),  make changes to it, and then save it, is there a
possibility that another user (on a different thread, maybe a faster
machine) could read the same record after me, but update it before
me?  If so, then the other user's changes would be lost (a data
concurrency problem).

How does Rails prevent this?

Is there a way to do something like
Model.save_only_if_updated_at_is_unchanged ?
Is there a find_and_lock, find_for_update, or other similar function?

This is different than dirty records, because it is comparing the
record I have in my session to the same record in the database to
ensure that  *no one else*  changed it.

Thank you,

Steve
Frederick C. (Guest)
on 2009-01-17 18:51
(Received via mailing list)
On 17 Jan 2009, at 16:45, srj wrote:

>
> In Rails 2.2.2, if I retrieve a record using a Model.find method (any
> of the finds),  make changes to it, and then save it, is there a
> possibility that another user (on a different thread, maybe a faster
> machine) could read the same record after me, but update it before
> me?  If so, then the other user's changes would be lost (a data
> concurrency problem).

Yes
>
>
> How does Rails prevent this?
>
> Is there a way to do something like
> Model.save_only_if_updated_at_is_unchanged ?
> Is there a find_and_lock, find_for_update, or other similar function?
>
Look up optimistic locking.

Fred
This topic is locked and can not be replied to.