Reputation: 267
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
Upvotes: 0
Views: 67
Reputation: 35350
Read about ActiveRecord::Transactions. You can wrap everything in a block
ActiveRecord::Base.transaction do
EntityUser.delete_all(:user_id => @current_user.id)
# ...
end
If something goes wrong, you can call raise ActiveRecord::Rollback
within that block to cause all changes to the database within the transaction block to be reverted.
Another thought is to soft-delete the EntityUser
instance(s) for a User
instead of actually removing them from the database. There are gems to help you with this functionality such as acts_as_paranoid
.
You would
EntityUser
instance(s)EntityUser
instance(s)If something goes wrong using this method it's easy to just un-delete the soft-deleted records.
Upvotes: 1