Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r7kamura/73633bd30ffc1647f61c97f3a4de8d67 to your computer and use it in GitHub Desktop.
Save r7kamura/73633bd30ffc1647f61c97f3a4de8d67 to your computer and use it in GitHub Desktop.
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 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