ruby on rails - ActiveRecord transaction not rolling back on failure -
i trying use activerecord::base.transaction ensure customer, customeraccount , stocknotification created @ once, or none gets created @ if 1 of them fails
here transaction
stock_notification.rb:
validates_presence_of :email def self.make parameters activerecord::base.transaction shop_id = productvariant.find(parameters[:product_variant_id]).shop_id parameters[:customer_account_id] = customeraccount.find_or_make!(parameters[:email], shop_id).id @stock_notification = stocknotification.create(parameters) # reference end @stock_notification end you might need
customer_account.rb:
def self.find_or_make! email, shop_id customer = customer.where(email: email).first_or_create! customeraccount.where(shop_id: shop_id, customer_id: customer.id).first_or_create! end if call stocknotification.make blank email, create fails (reference a) , no stock notification created, problem customer/customeraccount still being created.
so transaction not doing it's job @ all, or missing something?
yep. transaction fail if exception raised. should use not create, create! in case of failure. see here.
Comments
Post a Comment