rails: `build_association` on `belongs_to` does not add the builder to has_many association

Steps to reproduce

When build_association is called on a belongs_to relation, then the model does not get added to the inverse has_many relation. This behavior can also be observed when assigning a new record directly to the belong_to association.

# frozen_string_literal: true

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "activerecord", "5.1.5"
  gem "sqlite3"
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, force: true do |t|
  end

  create_table :authors, force: true do |t|
    t.integer :post_id
  end
end

class Post < ActiveRecord::Base
  has_many :authors

  validates :authors, length: { minimum: 1 }
end

class Author < ActiveRecord::Base
  belongs_to :post
end

class BugTest < Minitest::Test

  def test_build_association_adds_to_inverse
    author = Author.new

    post = author.build_post

    assert_equal 1, post.authors.length
    assert_equal author, post.authors.first
    assert_predicate author, :valid?
    assert_predicate post, :valid?
    assert author.save
  end

  def test_assigning_belongs_to_adds_to_inverse
    author = Author.new
    post = Post.new

    author.post = post

    assert_equal 1, post.authors.length
    assert_equal author, post.authors.first
    assert_predicate author, :valid?
    assert_predicate post, :valid?
    assert author.save
  end

end

Expected behavior

I expect the Post model to know about the author, through which it was built. This would match the behavior observed, when used the other way round. (post.authors.build actually assigns the inverse post and post = authors.build_post results in authors.post == post being true)

Actual behavior

The authors relation is blank. Hence, the validations on the Post model are failing. Neither am I able to then save Post nor the Author model, without me explicitly adding the Author model to the post.authors relation.

System configuration

Rails version: 5.1.5 Ruby version: 2.3.6

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 4
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Is this still an issue would like to pick this up.

Results for gem "activerecord", "4.2.0" still not passing https://gist.github.com/victorhazbun/195f4d8a8043584e10fd60639ec51908

@victorhazbun could you test on 4.2.0 please?

I can confirm its a bug as it worked as expected on rails 4.2 and now relations are broken, which argument does it need in addition?

Reproduces on 5.1.2 when migrated from 4.2.1 and it has broken all application - that’s a disaster!!!