Quirky has_many through behavior when creating

I’ve bumped into this behavior many times, and always find it surprising. I don’t know if it’s a bug, or intended, but I would expect this to behave differently.

Given a blog, which has many posts & many comments through posts; when creating a new blog, assigning a preexisting post to its collection works fine, but the comments are “empty” from the blog’s perspective. This causes confusing behavior when attempting to add validations e.g. on whether a blog could be created with a post given some conditions of its comments, as it’s not possible to access the comments directly. It works on update though.

# https://github.com/rails/rails/pull/29619#issuecomment-392583498
# Better approach than http://codesnik.github.io/rails/2015/09/03/activerecord-and-exists-subqueries.html
require 'bundler/inline'

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

  gem 'rails', '6.0.3'
  gem 'pg'
  gem 'pry'

require 'active_record'
require 'minitest/autorun'
require 'logger'

ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'test_exists')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :blogs, force: true do |t|

  create_table :posts, force: true do |t|
    t.references :blog
    t.string :body

  create_table :comments, force: true do |t|
    t.references :post
    t.string :text

class Blog < ActiveRecord::Base
  has_many :posts
  has_many :comments, through: :posts

class Post < ActiveRecord::Base
  has_many :comments

class Comment < ActiveRecord::Base
  belongs_to :post

class PostCommentsExistsInnerTest < ActiveSupport::TestCase
  def setup
    @post = Post.create!
    @comment = @post.comments.create!(text: 'Foo')

    @blog = Blog.new
    @blog.posts << @post

  def test_post_is_included
    assert_includes @blog.posts, @post

  def test_comment_is_included_through_posts
    assert_includes @blog.posts[0].comments, @comment

  def test_comment_is_included
    assert_includes @blog.comments, @comment
    # fails on create, works if @blog is persisted.

Good catch! My guess is that this has something to do with trying to avoid unnecessary database roundtrips, but that adding a warning might at least be possible.

I see you what you’re saying, if all associated comments were loaded it’d need a roundtrip to the db, however In this case the associated record is already in memory, as asserted in the second test, and that’s what’s unexpected for me :slight_smile: