ActiveRecord transaction is not rolling back whole transaction

Syafiq Kamarul Azman

I've got a ticket model and users can buy tickets in series starting from a given serial number. The users choose the number of tickets they want to buy and the controller generates that number of tickets and stores in the database. However if one ticket fails to save, the transaction should roll back but in this case it is not.

tickets_controller.rb

...
def create
  number = params[:ticket_qty].to_i
  @tickets = Array.new
  number.times do |n|
    t = Ticket.new(ticket_params)
    t.serial_number = t.serial_number.to_i + n 
    @tickets.push(t)
  end

  respond_to do |format|
    ActiveRecord::Base.transaction do 
      @tickets.each do |t| 
        if t.save
          format.html { redirect_to tickets_path, notice: "#{number} #{"ticket".pluralize(number)} successfully created." }
          format.json { render :show, status: :created, location: tickets_path }
        else
          format.html { render :new, notice: "Some tickets have errors, check the serial number range" }
          format.json { render json: @ticket.errors, status: :unprocessable_entity }
        end
      end
    end
  end
end
...

Also the redirection is correct but no notice is showing up.

apneadiving

to have your transaction rolled back in case of error, replace t.save with t.save!

This would result in:

respond_to do |format|
  begin 
    ActiveRecord::Base.transaction do 
      @tickets.each &:save!
      format.html { redirect_to tickets_path, notice: "#{number} #{"ticket".pluralize(number)} successfully created." }
      format.json { render :show, status: :created, location: tickets_path }
    end
  rescue ActiveRecord::ActiveRecordError => e
    format.html { render :new, notice: "Some tickets have errors, check the serial number range" }
    format.json { render json: e.message, status: :unprocessable_entity }
  end
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails transaction not rolling back

From Dev

PetaPoco transaction not rolling back

From Dev

Cordova SQLite transaction not rolling back

From Dev

Rolling back transaction with Oracle OCCI

From Dev

Transaction in stored procedure not rolling back

From Dev

MySQL transaction not rolling back on error

From Dev

Prevent inner transaction from rolling back outer transaction

From Dev

Kafka Transaction Manager sends to Kafka Broker despite transaction rolling back

From Dev

Prevent inner transaction from rolling back outer transaction

From Dev

Laravel DB::transaction not rolling back on exception

From Dev

Rolling back a transaction in apache chemistry cmis

From Dev

Rolling back transaction scope C#

From Dev

Rolling back a transaction in apache chemistry cmis

From Dev

What is a good way of rolling back a transaction in Postgres

From Dev

How to prevent NServiceBus from rolling back the transaction or parts of it?

From Dev

SQL Server - Rolling back particular transaction only at a later date

From Dev

Exceptions when rolling back a transaction - connection already closed?

From Dev

How to prevent a specific INSERT statement from rolling back in a transaction

From Dev

TSQL: Prevent trigger suppressing error but rolling back transaction

From Dev

Rolling back Transaction Doesn't Work with TpFIB Components

From Dev

How to prevent NServiceBus from rolling back the transaction or parts of it?

From Dev

Jms-message-driven channel adaper not rolling back the transaction

From Dev

How to prevent a specific INSERT statement from rolling back in a transaction

From Dev

Why is my MSDTC transaction not correctly rolling back on my localhost environment?

From Dev

Validating the Presence of an ActiveRecord Transaction

From Dev

Transaction is not rolled back in ejb

From Dev

Transaction is not rolled back in ejb

From Dev

@transaction - roll back failing

From Dev

Ensuring a transaction within a method with ActiveRecord

Related Related

  1. 1

    Rails transaction not rolling back

  2. 2

    PetaPoco transaction not rolling back

  3. 3

    Cordova SQLite transaction not rolling back

  4. 4

    Rolling back transaction with Oracle OCCI

  5. 5

    Transaction in stored procedure not rolling back

  6. 6

    MySQL transaction not rolling back on error

  7. 7

    Prevent inner transaction from rolling back outer transaction

  8. 8

    Kafka Transaction Manager sends to Kafka Broker despite transaction rolling back

  9. 9

    Prevent inner transaction from rolling back outer transaction

  10. 10

    Laravel DB::transaction not rolling back on exception

  11. 11

    Rolling back a transaction in apache chemistry cmis

  12. 12

    Rolling back transaction scope C#

  13. 13

    Rolling back a transaction in apache chemistry cmis

  14. 14

    What is a good way of rolling back a transaction in Postgres

  15. 15

    How to prevent NServiceBus from rolling back the transaction or parts of it?

  16. 16

    SQL Server - Rolling back particular transaction only at a later date

  17. 17

    Exceptions when rolling back a transaction - connection already closed?

  18. 18

    How to prevent a specific INSERT statement from rolling back in a transaction

  19. 19

    TSQL: Prevent trigger suppressing error but rolling back transaction

  20. 20

    Rolling back Transaction Doesn't Work with TpFIB Components

  21. 21

    How to prevent NServiceBus from rolling back the transaction or parts of it?

  22. 22

    Jms-message-driven channel adaper not rolling back the transaction

  23. 23

    How to prevent a specific INSERT statement from rolling back in a transaction

  24. 24

    Why is my MSDTC transaction not correctly rolling back on my localhost environment?

  25. 25

    Validating the Presence of an ActiveRecord Transaction

  26. 26

    Transaction is not rolled back in ejb

  27. 27

    Transaction is not rolled back in ejb

  28. 28

    @transaction - roll back failing

  29. 29

    Ensuring a transaction within a method with ActiveRecord

HotTag

Archive