Last active
August 2, 2020 17:00
-
-
Save chrismaximin/66d6f2197b26067e67767e1266edcb79 to your computer and use it in GitHub Desktop.
ActiveRecord's after_commit callback triggered once per row
This file contains hidden or 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
begin | |
require 'bundler/inline' | |
rescue LoadError => e | |
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
raise e | |
end | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'activerecord', "6.0.3" | |
gem 'sqlite3' | |
end | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | |
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :post_items, force: true do |t| | |
t.string :title | |
end | |
end | |
class PostItem < ActiveRecord::Base | |
after_commit :my_callback, on: :update | |
private | |
def my_callback | |
puts "#{id}: previous_changes = #{previous_changes.inspect}" | |
end | |
end | |
class BugTest < Minitest::Test | |
def test_stuff | |
PostItem.create! | |
PostItem.create! | |
PostItem.transaction do | |
PostItem.first.update!(title: "first post title update 1") | |
PostItem.first.update!(title: "first post title update 2") | |
PostItem.last.update!(title: "last post title update 1") | |
PostItem.last.update!(title: "last post title update 2") | |
end | |
end | |
end |
This file contains hidden or 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
D, [2020-08-02T18:00:29.208603 #5622] DEBUG -- : (0.1ms) begin transaction | |
D, [2020-08-02T18:00:29.208798 #5622] DEBUG -- : PostItem Create (0.1ms) INSERT INTO "post_items" DEFAULT VALUES | |
D, [2020-08-02T18:00:29.209030 #5622] DEBUG -- : (0.0ms) commit transaction | |
D, [2020-08-02T18:00:29.209380 #5622] DEBUG -- : (0.0ms) begin transaction | |
D, [2020-08-02T18:00:29.209490 #5622] DEBUG -- : PostItem Create (0.1ms) INSERT INTO "post_items" DEFAULT VALUES | |
D, [2020-08-02T18:00:29.209598 #5622] DEBUG -- : (0.0ms) commit transaction | |
D, [2020-08-02T18:00:29.210048 #5622] DEBUG -- : (0.0ms) begin transaction | |
D, [2020-08-02T18:00:29.210218 #5622] DEBUG -- : PostItem Load (0.1ms) SELECT "post_items".* FROM "post_items" ORDER BY "post_items"."id" ASC LIMIT ? [["LIMIT", 1]] | |
D, [2020-08-02T18:00:29.210661 #5622] DEBUG -- : PostItem Update (0.0ms) UPDATE "post_items" SET "title" = ? WHERE "post_items"."id" = ? [["title", "first post title update 1"], ["id", 1]] | |
D, [2020-08-02T18:00:29.210884 #5622] DEBUG -- : PostItem Load (0.0ms) SELECT "post_items".* FROM "post_items" ORDER BY "post_items"."id" ASC LIMIT ? [["LIMIT", 1]] | |
D, [2020-08-02T18:00:29.211132 #5622] DEBUG -- : PostItem Update (0.0ms) UPDATE "post_items" SET "title" = ? WHERE "post_items"."id" = ? [["title", "first post title update 2"], ["id", 1]] | |
D, [2020-08-02T18:00:29.211340 #5622] DEBUG -- : PostItem Load (0.0ms) SELECT "post_items".* FROM "post_items" ORDER BY "post_items"."id" DESC LIMIT ? [["LIMIT", 1]] | |
D, [2020-08-02T18:00:29.211581 #5622] DEBUG -- : PostItem Update (0.0ms) UPDATE "post_items" SET "title" = ? WHERE "post_items"."id" = ? [["title", "last post title update 1"], ["id", 2]] | |
D, [2020-08-02T18:00:29.211746 #5622] DEBUG -- : PostItem Load (0.0ms) SELECT "post_items".* FROM "post_items" ORDER BY "post_items"."id" DESC LIMIT ? [["LIMIT", 1]] | |
D, [2020-08-02T18:00:29.211973 #5622] DEBUG -- : PostItem Update (0.0ms) UPDATE "post_items" SET "title" = ? WHERE "post_items"."id" = ? [["title", "last post title update 2"], ["id", 2]] | |
D, [2020-08-02T18:00:29.212094 #5622] DEBUG -- : (0.0ms) commit transaction | |
1: previous_changes = {"title"=>[nil, "first post title update 1"]} | |
2: previous_changes = {"title"=>[nil, "last post title update 1"]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment