Search code examples
ruby-on-railsruby-on-rails-3validationactiverecorddata-consistency

Rails consistency check on record update


I will start a system and we have chosed rails because it covers all our needs.

We need consistency on database updates in some point of the system and I remember that I found in some official documentation that rails can check for the versions of the records automatically, however I have googled a lot and I cannot find those pages again.

The problem is the following:

  • Suppose that there exists a record in a table of the database. Let's say, an "account".
  • Suppose that a user named Bob finds that record and starts editing it. Let's say that the user is changing its name from "Incomings" to "Sales".
  • Suppose that at the time Bob is editing the record, another user named Susan find exactly the same record and starts editing it. Let's say that Susan wants to change the name of the account from "Incomings" to "Service fees".
  • Then, Bob saves his changes.
  • After Bob saves his changes, Susan tries to save her changes.

At this point, the system should inform to Susan that another user has changed the record and her changes cannot be saved.

I can do this validation manually, but I remember that this can be done by Rails automatically. I remember that the only requeriment on the tables of the database where is required this validation is to add a "version" column and Rails will take care of this. Really I don't remember if the column must be called "version" or something else, but the fact is that only a columns enables this feature.

The problem is that I cannot find that documentation anymore. If anyone could provide me the link to that documentation I will be thankful, because the requeriment of the "version" column is all what I remember and I wan't to know what other things I must take care of.

Thanks in advance.


Solution

  • Well, I have found the answer to my own question.

    The documentation is on the API and can be found here:

    http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html

    The documentation is very short! And I was wrong on the name of the required column. The correct name of the column is 'lock_version'.

    Thank you to all who, at least, read my question.