![]() ![]() If an update request is made with a lower value in the lock_version field than is currently in the lock_version column in the database, the update request will fail with an ActiveRecord::StaleObjectError. Each time the record is updated, Active Record increments the lock_version column. In order to use optimistic locking, the table needs to have a column called lock_version of type integer. An ActiveRecord::StaleObjectError exception is thrown if that has occurred and the update is ignored. It does this by checking whether another process has made changes to a record since it was opened. Optimistic locking allows multiple users to access the same record for edits, and assumes a minimum of conflicts with the data. Locking is helpful for preventing race conditions when updating records in the database and ensuring atomic updates.Īctive Record provides two locking mechanisms: ![]() save CopyĪs customer is explicitly set to be a readonly object, the above code will raise an ActiveRecord::ReadOnlyRecord exception when calling customer.save with an updated value of visits. Using the find method, you can retrieve the object corresponding to the specified primary key that matches any supplied options. Run after_find and then after_initialize callbacks, if any.Īctive Record provides several different ways of retrieving a single object. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |