lundi 25 mars 2019

Outer transaction rollback not working rails

I am trying to update few records in my database, but unlucky.

I want this manipulations to be atomic (using transaction). What i want is that I need to update few attributes in table 1 and few attributes in table 2.

If the update process fails in any of the table, i want all the changes that were made in both the tables to get rollbacked.

Below is my not working code:

    def make_settings(account,attributeToUpdate,attribute_type)
    A.transaction do
      B.transaction do
          if attribute_type == "ADVANCED"
          self.user.create_or_update_attribute(attributeToUpdate[0],attributeToUpdate[1].to_s)
          end
     if attribute_type == "BASIC"
        us = account.user.user_setting.reload
        us[attributeToUpdate[0]] = attributeToUpdate[1]
        us.save!
      end
    end
  end 
end

What is happening now:- If the attribute type is ADVANCED few attributes are updated successfully in table A. The flow then proceeds to update attributes in table B, but something goes wrong while doing that.

Now what i want is when the updation in table B fails, then the records that got added/updated in table A should also get deleted/updated back. But that is not happening.

Can anyone help ? Thanks

Aucun commentaire:

Enregistrer un commentaire