Skip to content

Instantly share code, notes, and snippets.

@doromones
Last active June 7, 2019 16:28
Show Gist options
  • Select an option

  • Save doromones/c38ff93110e26279a77bca8e564c0609 to your computer and use it in GitHub Desktop.

Select an option

Save doromones/c38ff93110e26279a77bca8e564c0609 to your computer and use it in GitHub Desktop.
# Running:
saved_changes
{
"status" => [
[0] 0,
[1] 3
],
"updated_at" => [
[0] 2019-06-07 16:27:36 UTC,
[1] 2019-06-07 16:27:36 UTC
]
}
changes
{
"status" => [
[0] 0,
[1] 3
],
"updated_at" => [
[0] 2019-06-07 16:27:36 UTC,
[1] 2019-06-07 16:27:36 UTC
]
}
saved_changes
{
"status" => [
[0] 0,
[1] 3
],
"updated_at" => [
[0] 2019-06-07 16:27:36 UTC,
[1] 2019-06-07 16:27:36 UTC
]
}
changes
{
"status" => [
[0] 0,
[1] 5
],
"updated_at" => [
[0] 2019-06-07 16:27:36 UTC,
[1] 2019-06-07 16:27:36 UTC
]
}
AFTER MAIN UPDATE
{}
{
"id" => 1,
"status" => 5,
"created_at" => 2019-06-07 16:27:36 UTC,
"updated_at" => 2019-06-07 16:27:36 UTC
}
# frozen_string_literal: true
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.1.0"
gem "sqlite3", '~> 1.3.6'
gem "awesome_print"
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::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.integer :status
t.timestamps null: false
end
end
ActiveSupport::Deprecation.behavior = ->(_, _) {}
class Post < ActiveRecord::Base
after_update :inspect_saved_changes
def inspect_saved_changes
puts "saved_changes"
ap saved_changes
puts "changes"
ap changes
if status != 5
update_attributes(status: 5)
end
end
end
class BugTest < Minitest::Test
def test_saved_changes
post = Post.create!(status: 0)
post.update(status: 3)
puts "AFTER MAIN UPDATE"
ap post.saved_changes
ap post.reload.attributes
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment