AASM does not trigger ActiveRecord::Rollback












0















I have approve event enum in Booking model. I have a before_validation in approve



in my model/booking.rb I added custom validation



    include AASM

before_validation :item_availability, if: :approved?

enum status: [:pending, :approved, :rejected, :on_loan, :returned]

aasm column: :status, enum: true do

state :pending, initial: true
state :approved
state :rejected
state :on_loan
state :returned

event :approve do
transitions from: :pending, to: :approved
end

event :reject do
transitions from: :pending, to: :rejected
end

event :on_loan do
transitions from: :approved, to: :on_loan
end

event :returned do
transitions from: :on_loan, to: :returned
end

end


private

def item_availability
if item.quantity.to_f < quantity.to_f
errors.add(:base, "Not enough quantity for #{item.name} only #{item.quantity} left")
false
end
end


and in my controller, I'm calling the service



 @service = Manage::BookingApprovalService.new({booking: @booking})
@service.run


app/services/manage/booking_approval_service



class Manage::BookingApprovalService < BaseService

attr_accessor :booking

def run
Booking.transaction do
booking.approve! // I confirmed that I'm getting the false here
booking.item.decrement!(:quantity, booking.quantity)
BookingsMailer.send_approval(booking).deliver_now
end
end

end


I'm getting false when I debug the booking.approve! because the quantity I have in booking is greater than the item quantity.



But from the service. the decrement! and send_approval mail is still invoking.



Why does the service doesn't rollback if I'm getting false from booking.approve!










share|improve this question





























    0















    I have approve event enum in Booking model. I have a before_validation in approve



    in my model/booking.rb I added custom validation



        include AASM

    before_validation :item_availability, if: :approved?

    enum status: [:pending, :approved, :rejected, :on_loan, :returned]

    aasm column: :status, enum: true do

    state :pending, initial: true
    state :approved
    state :rejected
    state :on_loan
    state :returned

    event :approve do
    transitions from: :pending, to: :approved
    end

    event :reject do
    transitions from: :pending, to: :rejected
    end

    event :on_loan do
    transitions from: :approved, to: :on_loan
    end

    event :returned do
    transitions from: :on_loan, to: :returned
    end

    end


    private

    def item_availability
    if item.quantity.to_f < quantity.to_f
    errors.add(:base, "Not enough quantity for #{item.name} only #{item.quantity} left")
    false
    end
    end


    and in my controller, I'm calling the service



     @service = Manage::BookingApprovalService.new({booking: @booking})
    @service.run


    app/services/manage/booking_approval_service



    class Manage::BookingApprovalService < BaseService

    attr_accessor :booking

    def run
    Booking.transaction do
    booking.approve! // I confirmed that I'm getting the false here
    booking.item.decrement!(:quantity, booking.quantity)
    BookingsMailer.send_approval(booking).deliver_now
    end
    end

    end


    I'm getting false when I debug the booking.approve! because the quantity I have in booking is greater than the item quantity.



    But from the service. the decrement! and send_approval mail is still invoking.



    Why does the service doesn't rollback if I'm getting false from booking.approve!










    share|improve this question



























      0












      0








      0








      I have approve event enum in Booking model. I have a before_validation in approve



      in my model/booking.rb I added custom validation



          include AASM

      before_validation :item_availability, if: :approved?

      enum status: [:pending, :approved, :rejected, :on_loan, :returned]

      aasm column: :status, enum: true do

      state :pending, initial: true
      state :approved
      state :rejected
      state :on_loan
      state :returned

      event :approve do
      transitions from: :pending, to: :approved
      end

      event :reject do
      transitions from: :pending, to: :rejected
      end

      event :on_loan do
      transitions from: :approved, to: :on_loan
      end

      event :returned do
      transitions from: :on_loan, to: :returned
      end

      end


      private

      def item_availability
      if item.quantity.to_f < quantity.to_f
      errors.add(:base, "Not enough quantity for #{item.name} only #{item.quantity} left")
      false
      end
      end


      and in my controller, I'm calling the service



       @service = Manage::BookingApprovalService.new({booking: @booking})
      @service.run


      app/services/manage/booking_approval_service



      class Manage::BookingApprovalService < BaseService

      attr_accessor :booking

      def run
      Booking.transaction do
      booking.approve! // I confirmed that I'm getting the false here
      booking.item.decrement!(:quantity, booking.quantity)
      BookingsMailer.send_approval(booking).deliver_now
      end
      end

      end


      I'm getting false when I debug the booking.approve! because the quantity I have in booking is greater than the item quantity.



      But from the service. the decrement! and send_approval mail is still invoking.



      Why does the service doesn't rollback if I'm getting false from booking.approve!










      share|improve this question
















      I have approve event enum in Booking model. I have a before_validation in approve



      in my model/booking.rb I added custom validation



          include AASM

      before_validation :item_availability, if: :approved?

      enum status: [:pending, :approved, :rejected, :on_loan, :returned]

      aasm column: :status, enum: true do

      state :pending, initial: true
      state :approved
      state :rejected
      state :on_loan
      state :returned

      event :approve do
      transitions from: :pending, to: :approved
      end

      event :reject do
      transitions from: :pending, to: :rejected
      end

      event :on_loan do
      transitions from: :approved, to: :on_loan
      end

      event :returned do
      transitions from: :on_loan, to: :returned
      end

      end


      private

      def item_availability
      if item.quantity.to_f < quantity.to_f
      errors.add(:base, "Not enough quantity for #{item.name} only #{item.quantity} left")
      false
      end
      end


      and in my controller, I'm calling the service



       @service = Manage::BookingApprovalService.new({booking: @booking})
      @service.run


      app/services/manage/booking_approval_service



      class Manage::BookingApprovalService < BaseService

      attr_accessor :booking

      def run
      Booking.transaction do
      booking.approve! // I confirmed that I'm getting the false here
      booking.item.decrement!(:quantity, booking.quantity)
      BookingsMailer.send_approval(booking).deliver_now
      end
      end

      end


      I'm getting false when I debug the booking.approve! because the quantity I have in booking is greater than the item quantity.



      But from the service. the decrement! and send_approval mail is still invoking.



      Why does the service doesn't rollback if I'm getting false from booking.approve!







      ruby-on-rails aasm






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 9:47







      Allen Chun

















      asked Nov 15 '18 at 9:41









      Allen ChunAllen Chun

      1,6821937




      1,6821937
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The transaction block will rollback only if you raise an exception.



          def run
          Booking.transaction do
          fail(ActiveRecord::Rollback) unless booking.approve!
          booking.item.decrement!(:quantity, booking.quantity)
          BookingsMailer.send_approval(booking).deliver_now
          end
          end


          Docs: https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316467%2faasm-does-not-trigger-activerecordrollback%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The transaction block will rollback only if you raise an exception.



            def run
            Booking.transaction do
            fail(ActiveRecord::Rollback) unless booking.approve!
            booking.item.decrement!(:quantity, booking.quantity)
            BookingsMailer.send_approval(booking).deliver_now
            end
            end


            Docs: https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html






            share|improve this answer




























              1














              The transaction block will rollback only if you raise an exception.



              def run
              Booking.transaction do
              fail(ActiveRecord::Rollback) unless booking.approve!
              booking.item.decrement!(:quantity, booking.quantity)
              BookingsMailer.send_approval(booking).deliver_now
              end
              end


              Docs: https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html






              share|improve this answer


























                1












                1








                1







                The transaction block will rollback only if you raise an exception.



                def run
                Booking.transaction do
                fail(ActiveRecord::Rollback) unless booking.approve!
                booking.item.decrement!(:quantity, booking.quantity)
                BookingsMailer.send_approval(booking).deliver_now
                end
                end


                Docs: https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html






                share|improve this answer













                The transaction block will rollback only if you raise an exception.



                def run
                Booking.transaction do
                fail(ActiveRecord::Rollback) unless booking.approve!
                booking.item.decrement!(:quantity, booking.quantity)
                BookingsMailer.send_approval(booking).deliver_now
                end
                end


                Docs: https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 10:30









                RodrigoRodrigo

                3,57653168




                3,57653168
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316467%2faasm-does-not-trigger-activerecordrollback%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine