Skip to content

Instantly share code, notes, and snippets.

@yosiat
Last active May 31, 2018 20:58
Show Gist options
  • Save yosiat/2394bb7d660a99a69c777154b4246f59 to your computer and use it in GitHub Desktop.
Save yosiat/2394bb7d660a99a69c777154b4246f59 to your computer and use it in GitHub Desktop.
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"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "activerecord", "5.0.6"
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.boolean :admin
t.text :external_id
end
end
class User < ActiveRecord::Base
after_create do
update(external_id: "new string") unless external_id?
end
after_create do
if respond_to?(:saved_changes)
puts "Saved Changes: #{saved_changes}"
end
if respond_to?(:previous_changes)
puts "Preiovus Changes: #{previous_changes}"
end
end
end
ActiveRecord::Base.logger = Logger.new(STDOUT)
class BugTest < Minitest::Test
def test_association_stuff
user = User.create!(admin: true)
end
end
@yosiat
Copy link
Author

yosiat commented May 31, 2018

ActiveRecord 5.0.6

id, admin and external_id are in the changes

D, [2018-05-31T23:54:07.003504 #35160] DEBUG -- :    (0.0ms)  begin transaction
D, [2018-05-31T23:54:07.004022 #35160] DEBUG -- :   SQL (0.0ms)  INSERT INTO "users" ("admin") VALUES (?)  [["admin", "t"]]
D, [2018-05-31T23:54:07.004657 #35160] DEBUG -- :   SQL (0.0ms)  UPDATE "users" SET "id" = ?, "admin" = ?, "external_id" = ? WHERE "users"."id" = ?  [["id", 1], ["admin", "t"], ["external_id", "new string"], ["id", 1]]

Previous Changes: {"id"=>[nil, 1], "admin"=>[nil, true], "external_id"=>[nil, "new string"]}
D, [2018-05-31T23:54:07.004910 #35160] DEBUG -- :    (0.0ms)  commit transaction

ActiveRecord 5.1.6

Saved Changes which is new approach has only external_id without id & admin

D, [2018-05-31T23:56:18.767822 #35720] DEBUG -- :    (0.0ms)  begin transaction
D, [2018-05-31T23:56:18.768494 #35720] DEBUG -- :   SQL (0.1ms)  INSERT INTO "users" ("admin") VALUES (?)  [["admin", "t"]]
D, [2018-05-31T23:56:18.769149 #35720] DEBUG -- :   SQL (0.1ms)  UPDATE "users" SET "external_id" = ? WHERE "users"."id" = ?  [["external_id", "new string"], ["id", 1]]
Saved Changes: {"external_id"=>[nil, "new string"]}
DEPRECATION WARNING: The behavior of `previous_changes` inside of after callbacks is
deprecated without replacement. In the next release of Rails,
this method inside of `after_save` will return the changes that
were just saved.
 (called from block in <class:User> at cb.rb:46)
Preiovus Changes: {"id"=>[nil, 1], "admin"=>[nil, true]}
D, [2018-05-31T23:56:18.769461 #35720] DEBUG -- :    (0.0ms)  commit transaction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment