Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "activerecord", "6.0.0.beta1", require: "active_record"
gem "sqlite3"
end
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: ":memory:",
)
ActiveRecord::Schema.define do
create_table :articles
create_table :tags do |t|
t.references :article
t.references :user
end
create_table :users
end
class Article < ActiveRecord::Base
has_many :tags
has_many :users, through: :tags
end
class Tag < ActiveRecord::Base
belongs_to :article
belongs_to :user
validates :article_id, presence: true
validates :user_id, presence: true
end
class User < ActiveRecord::Base
has_many :tags
has_many :articles, through: :tags
end
require "minitest/autorun"
class ExampleTest < Minitest::Test
def test_create
User.create!.articles.create!
end
end
@r7kamura
Copy link
Author

r7kamura commented Jan 29, 2019

6.0.0.beta1

Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using concurrent-ruby 1.1.4
Using i18n 1.5.3
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using activesupport 6.0.0.beta1
Using activemodel 6.0.0.beta1
Using activerecord 6.0.0.beta1
Using bundler 2.0.1
Using sqlite3 1.3.13
-- create_table(:articles)
D, [2019-01-30T07:30:28.527301 #86413] DEBUG -- :    (0.8ms)  SELECT sqlite_version(*)
D, [2019-01-30T07:30:28.528133 #86413] DEBUG -- :    (0.2ms)  CREATE TABLE "articles" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL)
   -> 0.0035s
-- create_table(:tags)
D, [2019-01-30T07:30:28.528666 #86413] DEBUG -- :    (0.1ms)  CREATE TABLE "tags" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "article_id" integer, "user_id" integer)
D, [2019-01-30T07:30:28.529073 #86413] DEBUG -- :    (0.1ms)  CREATE  INDEX "index_tags_on_article_id" ON "tags" ("article_id")
D, [2019-01-30T07:30:28.529583 #86413] DEBUG -- :    (0.1ms)  CREATE  INDEX "index_tags_on_user_id" ON "tags" ("user_id")
   -> 0.0014s
-- create_table(:users)
D, [2019-01-30T07:30:28.530018 #86413] DEBUG -- :    (0.1ms)  CREATE TABLE "users" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL)
   -> 0.0004s
D, [2019-01-30T07:30:28.549389 #86413] DEBUG -- :    (0.2ms)  CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
D, [2019-01-30T07:30:28.555337 #86413] DEBUG -- :   ActiveRecord::InternalMetadata Load (0.1ms)  SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ?  [["key", "environment"], ["LIMIT", 1]]
D, [2019-01-30T07:30:28.558569 #86413] DEBUG -- :    (0.0ms)  begin transaction
D, [2019-01-30T07:30:28.558817 #86413] DEBUG -- :   ActiveRecord::InternalMetadata Create (0.1ms)  INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["key", "environment"], ["value", "default_env"], ["created_at", "2019-01-29 22:30:28.558292"], ["updated_at", "2019-01-29 22:30:28.558292"]]
D, [2019-01-30T07:30:28.559011 #86413] DEBUG -- :    (0.1ms)  commit transaction
Run options: --seed 45279

# Running:

D, [2019-01-30T07:30:28.586018 #86413] DEBUG -- :    (0.0ms)  begin transaction
D, [2019-01-30T07:30:28.586235 #86413] DEBUG -- :   User Create (0.1ms)  INSERT INTO "users" DEFAULT VALUES
D, [2019-01-30T07:30:28.586567 #86413] DEBUG -- :    (0.0ms)  commit transaction
E

Finished in 0.023019s, 43.4424 runs/s, 0.0000 assertions/s.

  1) Error:
ExampleTest#test_create:
ActiveRecord::RecordInvalid: Validation failed: Tags is invalid
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/validations.rb:80:in `raise_validation_error'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/validations.rb:52:in `save!'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/transactions.rb:313:in `block in save!'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/transactions.rb:374:in `block in with_transaction_returning_status'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/connection_adapters/abstract/database_statements.rb:255:in `transaction'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/transactions.rb:212:in `transaction'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/transactions.rb:372:in `with_transaction_returning_status'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/transactions.rb:313:in `save!'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/suppressor.rb:48:in `save!'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/collection_association.rb:362:in `insert_record'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/has_many_association.rb:36:in `insert_record'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/has_many_through_association.rb:28:in `insert_record'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/collection_association.rb:348:in `block (2 levels) in _create_record'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/collection_association.rb:445:in `replace_on_target'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/collection_association.rb:286:in `add_to_target'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/collection_association.rb:347:in `block in _create_record'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/collection_association.rb:136:in `block in transaction'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/connection_adapters/abstract/database_statements.rb:257:in `block in transaction'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/connection_adapters/abstract/transaction.rb:269:in `block in within_new_transaction'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/connection_adapters/abstract/transaction.rb:267:in `within_new_transaction'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/connection_adapters/abstract/database_statements.rb:257:in `transaction'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/transactions.rb:212:in `transaction'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/collection_association.rb:135:in `transaction'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/collection_association.rb:345:in `_create_record'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/has_many_association.rb:124:in `_create_record'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/association.rb:178:in `create!'
    /Users/r7kamura/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-6.0.0.beta1/lib/active_record/associations/collection_proxy.rb:366:in `create!'
    /Users/r7kamura/Desktop/test.rb:50:in `test_create'

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

5.2.2

Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using concurrent-ruby 1.1.4
Using i18n 1.5.3
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using activesupport 5.2.2
Using activemodel 5.2.2
Using arel 9.0.0
Using activerecord 5.2.2
Using bundler 2.0.1
Using sqlite3 1.3.13
-- create_table(:articles)
D, [2019-01-30T07:33:22.245477 #86440] DEBUG -- :    (1.1ms)  SELECT sqlite_version(*)
D, [2019-01-30T07:33:22.246197 #86440] DEBUG -- :    (0.6ms)  CREATE TABLE "articles" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL)
   -> 0.0465s
-- create_table(:tags)
D, [2019-01-30T07:33:22.246943 #86440] DEBUG -- :    (0.3ms)  CREATE TABLE "tags" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "article_id" integer, "user_id" integer)
D, [2019-01-30T07:33:22.247567 #86440] DEBUG -- :    (0.1ms)  CREATE  INDEX "index_tags_on_article_id" ON "tags" ("article_id")
D, [2019-01-30T07:33:22.248249 #86440] DEBUG -- :    (0.1ms)  CREATE  INDEX "index_tags_on_user_id" ON "tags" ("user_id")
   -> 0.0020s
-- create_table(:users)
D, [2019-01-30T07:33:22.248583 #86440] DEBUG -- :    (0.1ms)  CREATE TABLE "users" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL)
   -> 0.0003s
D, [2019-01-30T07:33:22.275311 #86440] DEBUG -- :    (0.1ms)  CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
D, [2019-01-30T07:33:22.285801 #86440] DEBUG -- :   ActiveRecord::InternalMetadata Load (0.2ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ?  [["key", "environment"], ["LIMIT", 1]]
D, [2019-01-30T07:33:22.288994 #86440] DEBUG -- :    (0.0ms)  begin transaction
D, [2019-01-30T07:33:22.289642 #86440] DEBUG -- :   ActiveRecord::InternalMetadata Create (0.1ms)  INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["key", "environment"], ["value", "default_env"], ["created_at", "2019-01-29 22:33:22.289283"], ["updated_at", "2019-01-29 22:33:22.289283"]]
D, [2019-01-30T07:33:22.289829 #86440] DEBUG -- :    (0.1ms)  commit transaction
Run options: --seed 62416

# Running:

D, [2019-01-30T07:33:22.324822 #86440] DEBUG -- :    (0.0ms)  begin transaction
D, [2019-01-30T07:33:22.325298 #86440] DEBUG -- :   User Create (0.0ms)  INSERT INTO "users" DEFAULT VALUES
D, [2019-01-30T07:33:22.325561 #86440] DEBUG -- :    (0.1ms)  commit transaction
D, [2019-01-30T07:33:22.338768 #86440] DEBUG -- :    (0.0ms)  begin transaction
D, [2019-01-30T07:33:22.339263 #86440] DEBUG -- :   Article Create (0.0ms)  INSERT INTO "articles" DEFAULT VALUES
D, [2019-01-30T07:33:22.344286 #86440] DEBUG -- :   Tag Create (0.1ms)  INSERT INTO "tags" ("article_id", "user_id") VALUES (?, ?)  [["article_id", 1], ["user_id", 1]]
D, [2019-01-30T07:33:22.344449 #86440] DEBUG -- :    (0.0ms)  commit transaction
.

Finished in 0.022948s, 43.5768 runs/s, 0.0000 assertions/s.

1 runs, 0 assertions, 0 failures, 0 errors, 0 skips

@r7kamura
Copy link
Author

r7kamura commented Jan 29, 2019

@r7kamura
Copy link
Author

r7kamura commented Jan 30, 2019

6.0.0.beta1 だと Tag の validation のタイミングで article_id が nil になっている。Article 保存前の _run_validate_callbacks で Tag の validation も実行されてエラーになる。INSERT INTO articles も結局実行されない。

#<Tag:0x00007feb87bc4650 id: nil, article_id: nil, user_id: 1>

5.2.2 では nil ではない。

#<Tag:0x00007f91854a4c28 id: nil, article_id: 1, user_id: 1>

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