Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Bug: Active Record suppression does not apply to `create_*` methods provided by `has_one` associations
gclaghorn ~/Rails/sandbox$ ruby suppression_has_one.rb
-- create_table(:posts, {:force=>true})
D, [2015-06-03T23:24:02.454609 #22736] DEBUG -- : (0.3ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)
-> 0.0061s
-- create_table(:comments, {:force=>true})
D, [2015-06-03T23:24:02.455380 #22736] DEBUG -- : (0.1ms) CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "post_id" integer)
-> 0.0006s
Run options: --seed 24491
# Running:
D, [2015-06-03T23:24:02.509439 #22736] DEBUG -- : (0.1ms) SELECT COUNT(*) FROM "comments"
D, [2015-06-03T23:24:02.513131 #22736] DEBUG -- : (0.1ms) begin transaction
D, [2015-06-03T23:24:02.514155 #22736] DEBUG -- : SQL (0.1ms) INSERT INTO "posts" DEFAULT VALUES
D, [2015-06-03T23:24:02.514869 #22736] DEBUG -- : (0.1ms) commit transaction
D, [2015-06-03T23:24:02.524158 #22736] DEBUG -- : (0.1ms) begin transaction
D, [2015-06-03T23:24:02.526363 #22736] DEBUG -- : SQL (0.1ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", 1]]
D, [2015-06-03T23:24:02.526771 #22736] DEBUG -- : (0.0ms) commit transaction
D, [2015-06-03T23:24:02.529352 #22736] DEBUG -- : Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? LIMIT 1 [["post_id", 1]]
D, [2015-06-03T23:24:02.530860 #22736] DEBUG -- : (0.1ms) SELECT COUNT(*) FROM "comments"
F
Finished in 0.030272s, 33.0343 runs/s, 33.0343 assertions/s.
1) Failure:
SuppressionTest#test_applies_to_singular_association_methods [suppression_has_one.rb:42]:
#<Proc:0x007fe33c3a5e40@suppression_has_one.rb:42 (lambda)> didn't change by 0.
Expected: 0
Actual: 1
1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# 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|
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_one :comment
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class SuppressionTest < ActiveSupport::TestCase
def test_applies_to_singular_association_methods
assert_no_difference -> { Comment.count } do
Comment.suppress do
post = Post.create!
post.create_comment!
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment