Last active
August 4, 2020 07:00
-
-
Save danielfone/5d77d71313bf758d980e to your computer and use it in GitHub Desktop.
AASM after_commit behaviour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ruby 2.2.2 | |
# activerecord 4.2.3 | |
# aasm 4.2.0 | |
require 'active_record' | |
require 'aasm' | |
# Setup a mock AR table | |
ActiveRecord::Migration.verbose = false | |
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") | |
ActiveRecord::Schema.define(:version => 1) do | |
create_table :orders do |t| | |
t.string :aasm_state | |
end | |
end | |
# Example record with scope | |
class Order < ActiveRecord::Base | |
include AASM | |
aasm do | |
state :draft, initial: true | |
state :placed | |
event :place, after_commit: :log_placed do | |
transitions from: :draft, to: :placed | |
end | |
end | |
def log_placed | |
logger.info "Order has been placed" | |
end | |
end | |
o = Order.create! | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
# | |
# The behaviour under question starts here | |
# | |
Order.transaction do | |
o.place! | |
raise ActiveRecord::Rollback | |
end | |
o.reload | |
o.logger.info o.aasm_state | |
# DEBUG -- : (0.1ms) begin transaction | |
# DEBUG -- : (0.0ms) SAVEPOINT active_record_1 | |
# DEBUG -- : SQL (0.1ms) UPDATE "orders" SET "aasm_state" = ? WHERE "orders"."id" = ? [["aasm_state", "placed"], ["id", 1]] | |
# DEBUG -- : (0.0ms) RELEASE SAVEPOINT active_record_1 | |
# INFO -- : Order has been placed | |
# DEBUG -- : (0.0ms) rollback transaction | |
# DEBUG -- : Order Load (0.1ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 1]] | |
# INFO -- : draft |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment