I’m developing a Rails application that has a background process that updates some user information. In order to do so, this method has to delete all the existing information (stored in another table) and get new information from the Internet. The problem is that if something goes wrong in the midtime users don’t have information until the process runs again.
there is something to do like:
transaction = EntityUser.delete_all(:user_id => @current_user.id)
#instructions that adds new entity
...
...
transaction.commit
can anyone suggest something that i can do to avoid this kind of problem?
thank you
Read about ActiveRecord::Transactions. You can wrap everything in a block
If something goes wrong, you can call
raise ActiveRecord::Rollbackwithin that block to cause all changes to the database within the transaction block to be reverted.Another thought is to soft-delete the
EntityUserinstance(s) for aUserinstead of actually removing them from the database. There are gems to help you with this functionality such asacts_as_paranoid.You would
EntityUserinstance(s)EntityUserinstance(s)If something goes wrong using this method it’s easy to just un-delete the soft-deleted records.