Skip to content

Instantly share code, notes, and snippets.

@BRMatt
Last active November 21, 2020 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BRMatt/ded04736f285d7cd04b3 to your computer and use it in GitHub Desktop.
Save BRMatt/ded04736f285d7cd04b3 to your computer and use it in GitHub Desktop.
# Activate the gem you are reporting the issue against.
gem 'activerecord', if ENV['RAILS_3'] then '3.2.19' else '4.1.6' 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 do |t|
end
create_table :comments do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_duping_association_on_new_record_returns_a_copy_of_the_association
post = Post.new
post.comments = original_comments = [Comment.create!, Comment.new]
assert_equal original_comments, post.comments
dup_comments = post.comments.dup
refute_equal post.comments.object_id, dup_comments.object_id
assert_equal original_comments, dup_comments
end
def test_duping_association_on_new_record_does_not_mutate_the_association_on_the_record
post = Post.new
post.comments = original_comments = [Comment.create!, Comment.new]
assert_equal original_comments, post.comments
dup_comments = post.comments.dup
refute_equal post.comments.object_id, dup_comments.object_id
assert_equal original_comments, post.comments
end
def test_calling_to_a_on_relation_returns_copy_of_association
post = Post.new
post.comments = original_comments = [Comment.create!, Comment.new]
dup_comments = post.comments.to_a
assert_equal original_comments, dup_comments
refute_equal post.comments.object_id, dup_comments.object_id
end
def test_getting_array_version_of_association_does_not_mutate_association_collection
post = Post.new
post.comments = original_comments = [Comment.create!, Comment.new]
dup_comments = post.comments.to_a
refute_equal post.comments.object_id, dup_comments.object_id
assert_equal original_comments, post.comments
end
def test_duping_association_on_existing_record_returns_a_copy_of_the_association
post = Post.create!
post.comments = original_comments = [Comment.create!, Comment.new]
assert_equal original_comments, post.comments
dup_comments = post.comments.dup
assert_equal original_comments, dup_comments
refute_equal post.comments.object_id, dup_comments.object_id
end
def test_duping_association_on_existing_record_does_not_mutate_the_association_on_the_record
post = Post.create!
post.comments = original_comments = [Comment.create!, Comment.new]
assert_equal original_comments, post.comments
post.comments.dup
assert_equal original_comments, post.comments
end
end
vagrant@precise64:/vagrant$ RAILS_3=1 ruby bug.rb
-- create_table(:posts)
D, [2014-09-30T14:15:15.790971 #2648] DEBUG -- : (0.4ms) select sqlite_version(*)
D, [2014-09-30T14:15:15.791456 #2648] DEBUG -- : (0.3ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)
-> 0.0060s
-- create_table(:comments)
D, [2014-09-30T14:15:15.792094 #2648] DEBUG -- : (0.1ms) CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "post_id" integer)
-> 0.0005s
Run options: --seed 14890
# Running:
D, [2014-09-30T14:15:15.821107 #2648] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:15.823968 #2648] DEBUG -- : SQL (1.4ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", nil]]
D, [2014-09-30T14:15:15.824300 #2648] DEBUG -- : (0.1ms) commit transaction
BugTest .D, [2014-09-30T14:15:15.829422 #2648] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:15.829982 #2648] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", nil]]
D, [2014-09-30T14:15:15.830267 #2648] DEBUG -- : (0.0ms) commit transaction
.D, [2014-09-30T14:15:15.830810 #2648] DEBUG -- : (0.1ms) begin transaction
D, [2014-09-30T14:15:15.831202 #2648] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", nil]]
D, [2014-09-30T14:15:15.831446 #2648] DEBUG -- : (0.1ms) commit transaction
.D, [2014-09-30T14:15:15.831932 #2648] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:15.832915 #2648] DEBUG -- : SQL (0.0ms) INSERT INTO "posts" VALUES(NULL)
D, [2014-09-30T14:15:15.833329 #2648] DEBUG -- : (0.0ms) commit transaction
D, [2014-09-30T14:15:15.833751 #2648] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:15.834385 #2648] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", nil]]
D, [2014-09-30T14:15:15.834642 #2648] DEBUG -- : (0.1ms) commit transaction
D, [2014-09-30T14:15:15.836786 #2648] DEBUG -- : Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 1
D, [2014-09-30T14:15:15.837246 #2648] DEBUG -- : (0.1ms) begin transaction
D, [2014-09-30T14:15:15.838127 #2648] DEBUG -- : (0.0ms) UPDATE "comments" SET "post_id" = 1 WHERE "comments"."id" = 4
D, [2014-09-30T14:15:15.838709 #2648] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", 1]]
D, [2014-09-30T14:15:15.838966 #2648] DEBUG -- : (0.1ms) commit transaction
.D, [2014-09-30T14:15:15.839483 #2648] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:15.839886 #2648] DEBUG -- : SQL (0.0ms) INSERT INTO "posts" VALUES(NULL)
D, [2014-09-30T14:15:15.840139 #2648] DEBUG -- : (0.1ms) commit transaction
D, [2014-09-30T14:15:15.840398 #2648] DEBUG -- : (0.1ms) begin transaction
D, [2014-09-30T14:15:15.840784 #2648] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", nil]]
D, [2014-09-30T14:15:15.841076 #2648] DEBUG -- : (0.1ms) commit transaction
D, [2014-09-30T14:15:15.841654 #2648] DEBUG -- : Comment Load (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 2
D, [2014-09-30T14:15:15.841912 #2648] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:15.842509 #2648] DEBUG -- : (0.0ms) UPDATE "comments" SET "post_id" = 2 WHERE "comments"."id" = 6
D, [2014-09-30T14:15:15.842979 #2648] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", 2]]
D, [2014-09-30T14:15:15.843260 #2648] DEBUG -- : (0.0ms) commit transaction
.D, [2014-09-30T14:15:15.843749 #2648] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:15.844100 #2648] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", nil]]
D, [2014-09-30T14:15:15.844372 #2648] DEBUG -- : (0.1ms) commit transaction
.
Finished in 0.042857s, 140.0009 runs/s, 350.0022 assertions/s.
6 runs, 15 assertions, 0 failures, 0 errors, 0 skips
vagrant@precise64:/vagrant$ ruby bug.rb
-- create_table(:posts)
D, [2014-09-30T14:15:27.557651 #2652] DEBUG -- : (0.3ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)
-> 0.0027s
-- create_table(:comments)
D, [2014-09-30T14:15:27.558346 #2652] DEBUG -- : (0.1ms) CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "post_id" integer)
-> 0.0004s
Run options: --seed 26165
# Running:
D, [2014-09-30T14:15:27.587359 #2652] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:27.590409 #2652] DEBUG -- : SQL (0.1ms) INSERT INTO "comments" DEFAULT VALUES
D, [2014-09-30T14:15:27.590776 #2652] DEBUG -- : (0.0ms) commit transaction
BugTest .D, [2014-09-30T14:15:27.596141 #2652] DEBUG -- : (0.1ms) begin transaction
D, [2014-09-30T14:15:27.597307 #2652] DEBUG -- : SQL (0.1ms) INSERT INTO "posts" DEFAULT VALUES
D, [2014-09-30T14:15:27.597574 #2652] DEBUG -- : (0.1ms) commit transaction
D, [2014-09-30T14:15:27.597853 #2652] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:27.598233 #2652] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" DEFAULT VALUES
D, [2014-09-30T14:15:27.598410 #2652] DEBUG -- : (0.0ms) commit transaction
D, [2014-09-30T14:15:27.599919 #2652] DEBUG -- : Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 1]]
D, [2014-09-30T14:15:27.600676 #2652] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:27.601542 #2652] DEBUG -- : SQL (0.1ms) UPDATE "comments" SET "post_id" = ? WHERE "comments"."id" = 2 [["post_id", 1]]
D, [2014-09-30T14:15:27.602084 #2652] DEBUG -- : SQL (0.1ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", 1]]
D, [2014-09-30T14:15:27.602283 #2652] DEBUG -- : (0.0ms) commit transaction
D, [2014-09-30T14:15:27.602804 #2652] DEBUG -- : Comment Load (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 1]]
.D, [2014-09-30T14:15:27.603269 #2652] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:27.603594 #2652] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" DEFAULT VALUES
D, [2014-09-30T14:15:27.603770 #2652] DEBUG -- : (0.0ms) commit transaction
.D, [2014-09-30T14:15:27.604315 #2652] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:27.604619 #2652] DEBUG -- : SQL (0.1ms) INSERT INTO "comments" DEFAULT VALUES
D, [2014-09-30T14:15:27.604793 #2652] DEBUG -- : (0.0ms) commit transaction
FD, [2014-09-30T14:15:27.611559 #2652] DEBUG -- : (0.1ms) begin transaction
D, [2014-09-30T14:15:27.612063 #2652] DEBUG -- : SQL (0.1ms) INSERT INTO "comments" DEFAULT VALUES
D, [2014-09-30T14:15:27.612275 #2652] DEBUG -- : (0.0ms) commit transaction
FD, [2014-09-30T14:15:27.615083 #2652] DEBUG -- : (0.1ms) begin transaction
D, [2014-09-30T14:15:27.615804 #2652] DEBUG -- : SQL (0.1ms) INSERT INTO "posts" DEFAULT VALUES
D, [2014-09-30T14:15:27.616140 #2652] DEBUG -- : (0.0ms) commit transaction
D, [2014-09-30T14:15:27.616439 #2652] DEBUG -- : (0.0ms) begin transaction
D, [2014-09-30T14:15:27.616876 #2652] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" DEFAULT VALUES
D, [2014-09-30T14:15:27.617194 #2652] DEBUG -- : (0.0ms) commit transaction
D, [2014-09-30T14:15:27.617745 #2652] DEBUG -- : Comment Load (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 2]]
D, [2014-09-30T14:15:27.617960 #2652] DEBUG -- : (0.1ms) begin transaction
D, [2014-09-30T14:15:27.618810 #2652] DEBUG -- : SQL (0.1ms) UPDATE "comments" SET "post_id" = ? WHERE "comments"."id" = 7 [["post_id", 2]]
D, [2014-09-30T14:15:27.619324 #2652] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", 2]]
D, [2014-09-30T14:15:27.619606 #2652] DEBUG -- : (0.1ms) commit transaction
D, [2014-09-30T14:15:27.620082 #2652] DEBUG -- : Comment Load (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 2]]
.
Finished in 0.036399s, 164.8411 runs/s, 412.1027 assertions/s.
1) Failure:
BugTest#test_duping_association_on_new_record_does_not_mutate_the_association_on_the_record [bug.rb:55]:
--- expected
+++ actual
@@ -1 +1 @@
-[#<Comment id: 5, post_id: nil>, #<Comment id: nil, post_id: nil>]
+#<ActiveRecord::Associations::CollectionProxy []>
2) Failure:
BugTest#test_duping_association_on_new_record_returns_a_copy_of_the_association [bug.rb:42]:
--- expected
+++ actual
@@ -1 +1 @@
-[#<Comment id: 6, post_id: nil>, #<Comment id: nil, post_id: nil>]
+#<ActiveRecord::Associations::CollectionProxy []>
6 runs, 15 assertions, 2 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment