简体   繁体   中英

Ruby on Rails: What is stale object and it's features?

In my application i got the error as con't update stale object. I don't know about this object. When it will create and why it was called a stale object. I want to know. Is it used in Only in ROR or other languges are also uses, this object and purpose?

According to the documentation: http://api.rubyonrails.org/classes/ActiveRecord/StaleObjectError.html

Reason of Stale object exception is:

Record is stale when it's being saved in another query after instantiation, for example, when two users edit the same wiki page and one starts editing and saves the page before the other.

Solution : To avoid this exception use the locking system: http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html

In my application multiple threads to be able to edit the same work -> version -> element record. its detecting error and raising the suitable error.

I typically did to handle the error:

I print a flash message such as "Sorry, please try after someone time, someone else editing this working version element".

        element.lock_version = Document::Element.find(element.id).lock_version
        element.save!

For example, such as detecting that one edit changed the element's type, where as the other edit changed the element's name by different creator, and then code of the two record should be merged done in one save.

In general, I used to element.reload to fetch a fresh version of the element.

worked fine!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM