AASM does not trigger ActiveRecord::Rollback
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
add a comment |
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
add a comment |
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
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
ruby-on-rails aasm
edited Nov 15 '18 at 9:47
Allen Chun
asked Nov 15 '18 at 9:41
Allen ChunAllen Chun
1,6821937
1,6821937
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 15 '18 at 10:30
RodrigoRodrigo
3,57653168
3,57653168
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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