{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/24758","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/24758/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/24758/comments","events_url":"https://api.github.com/repos/rails/rails/issues/24758/events","html_url":"https://github.com/rails/rails/issues/24758","id":151472064,"number":24758,"title":"Child association doesn't see parent record and fails with validation error on creation","user":{"login":"sevaorlov","id":1062645,"avatar_url":"https://avatars.githubusercontent.com/u/1062645?v=3","gravatar_id":"","url":"https://api.github.com/users/sevaorlov","html_url":"https://github.com/sevaorlov","followers_url":"https://api.github.com/users/sevaorlov/followers","following_url":"https://api.github.com/users/sevaorlov/following{/other_user}","gists_url":"https://api.github.com/users/sevaorlov/gists{/gist_id}","starred_url":"https://api.github.com/users/sevaorlov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sevaorlov/subscriptions","organizations_url":"https://api.github.com/users/sevaorlov/orgs","repos_url":"https://api.github.com/users/sevaorlov/repos","events_url":"https://api.github.com/users/sevaorlov/events{/privacy}","received_events_url":"https://api.github.com/users/sevaorlov/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-04-27T19:34:12Z","updated_at":"2016-04-29T17:10:45Z","closed_at":"2016-04-29T17:10:45Z","body":"### Steps to reproduce\r\n\r\nHi, the following code fails for Rails 4+ and master, but passes in Rails 3. Child association doesn't see that user will be created in current transaction.\r\n\r\n```ruby\r\nbegin\r\n  require 'bundler/inline'\r\nrescue LoadError => e\r\n  $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'\r\n  raise e\r\nend\r\n\r\ngemfile(true) do\r\n  source 'https://rubygems.org'\r\n  gem 'activerecord', '~> 4'\r\n  gem 'sqlite3'\r\n  gem 'minitest'\r\nend\r\n\r\nrequire 'active_record'\r\nrequire 'minitest/autorun'\r\nrequire 'logger'\r\n\r\n# Ensure backward compatibility with Minitest 4\r\nMinitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)\r\n\r\n# This connection will do for database-independent bug reports.\r\nActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')\r\nActiveRecord::Base.logger = Logger.new(STDOUT)\r\n\r\nActiveRecord::Schema.define do\r\n  create_table :users, force: true  do |t|\r\n  end\r\n\r\n  create_table :managers_reporters, force: true  do |t|\r\n    t.integer :manager_id\r\n    t.integer :reporter_id\r\n  end\r\nend\r\n\r\nclass User < ActiveRecord::Base\r\n  has_many :user_managers, foreign_key: :reporter_id, class_name: 'ManagersReporters'\r\n  has_many :managers, through: :user_managers\r\n\r\n  has_many :user_reporters, foreign_key: :manager_id, class_name: 'ManagersReporters'\r\n  has_many :reports, through: :user_reporters\r\nend\r\n\r\nclass ManagersReporters < ActiveRecord::Base\r\n  belongs_to :manager, class_name: 'User'\r\n  belongs_to :reporter,  class_name: 'User'\r\n\r\n  validates :manager, :reporter, presence: true\r\nend\r\n\r\nclass BugTest < Minitest::Test\r\n  def test_creation\r\n    manager = User.create!\r\n\r\n    # fails with 'ActiveRecord::RecordInvalid: Validation failed: User managers is invalid'\r\n    user = User.create! managers: [manager]\r\n\r\n    assert true\r\n  end\r\nend\r\n```\r\n\r\n### Expected behavior\r\nno error\r\n\r\n### Actual behavior\r\nvalidation error `ActiveRecord::RecordInvalid: Validation failed: User managers is invalid`\r\n\r\n### System configuration\r\n**Rails version**: 4+\r\n\r\n\r\nMaybe the problem is in using `foreign_keys`, because the following code doesn't fail\r\n```ruby\r\nbegin\r\n  require 'bundler/inline'\r\nrescue LoadError => e\r\n  $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'\r\n  raise e\r\nend\r\n\r\ngemfile(true) do\r\n  source 'https://rubygems.org'\r\n  gem 'activerecord', '~> 4'\r\n  gem 'sqlite3'\r\n  gem 'minitest'\r\nend\r\n\r\nrequire 'active_record'\r\nrequire 'minitest/autorun'\r\nrequire 'logger'\r\n\r\n# Ensure backward compatibility with Minitest 4\r\nMinitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)\r\n\r\n# This connection will do for database-independent bug reports.\r\nActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')\r\nActiveRecord::Base.logger = Logger.new(STDOUT)\r\n\r\nActiveRecord::Schema.define do\r\n  create_table :users, force: true  do |t|\r\n  end\r\n\r\n  create_table :contents, force: true  do |t|\r\n    t.integer :content_item_id\r\n  end\r\n\r\n  create_table :content_items, force: true  do |t|\r\n    t.integer :content_id\r\n    t.integer :user_id\r\n  end\r\nend\r\n\r\nclass User < ActiveRecord::Base\r\n  has_many :content_items\r\n  has_many :contents, through: :content_items\r\nend\r\n\r\nclass Content < ActiveRecord::Base\r\n  belongs_to :content_item\r\n\r\nend\r\n\r\nclass ContentItem < ActiveRecord::Base\r\n  belongs_to :content\r\n  belongs_to :user\r\n  validates :user, :content, presence: true\r\nend\r\n\r\nclass BugTest < Minitest::Test\r\n  def test_creation\r\n    content = Content.create!\r\n    User.create contents: [content]\r\n\r\n    assert true\r\n  end\r\nend\r\n```\r\n\r\n\r\nI'll investigate it\r\n"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/215816979","html_url":"https://github.com/rails/rails/issues/24758#issuecomment-215816979","issue_url":"https://api.github.com/repos/rails/rails/issues/24758","id":215816979,"user":{"login":"sevaorlov","id":1062645,"avatar_url":"https://avatars.githubusercontent.com/u/1062645?v=3","gravatar_id":"","url":"https://api.github.com/users/sevaorlov","html_url":"https://github.com/sevaorlov","followers_url":"https://api.github.com/users/sevaorlov/followers","following_url":"https://api.github.com/users/sevaorlov/following{/other_user}","gists_url":"https://api.github.com/users/sevaorlov/gists{/gist_id}","starred_url":"https://api.github.com/users/sevaorlov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sevaorlov/subscriptions","organizations_url":"https://api.github.com/users/sevaorlov/orgs","repos_url":"https://api.github.com/users/sevaorlov/repos","events_url":"https://api.github.com/users/sevaorlov/events{/privacy}","received_events_url":"https://api.github.com/users/sevaorlov/received_events","type":"User","site_admin":false},"created_at":"2016-04-29T17:10:45Z","updated_at":"2016-04-29T17:10:45Z","body":"@Erol, thanks, seems it is a duplicate, so I'll close it"}}