salsify/goldiloader

New instances of a tree like model wrongfully return true on the exists? method of the children association

Closed this issue · 1 comments

I hope the following testcase explains the problem better then any text i could write. The problem also seems to be limited to exists?, the children association itself returns an empty array.

Removing the goldiloader gem would make this test pass (just in case this questions comes to mind).

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'
  gem 'rails'#, github: 'rails/rails'
  gem 'goldiloader', github: 'salsify/goldiloader'
  gem 'sqlite3'
end

require 'active_record'
require 'minitest/autorun'
require 'logger'
# 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 :comments, force: true do |t|
    t.integer :parent_id
  end
end

class Comment < ActiveRecord::Base
  has_many :children, class_name: 'Comment', foreign_key: :parent_id
end

class BugTest < Minitest::Test
  def test_association_stuff
    Comment.create!
    assert !Comment.new.children.exists?
  end
end

PS: Thank you for this nice gem.

Thanks for the great test case! It looks like the behavior we were trying to mimic changed in Rails 4.0 since the test case fails in Rails 3.2 without goldiloader but passes with newer versions of Rails. The problem is https://github.com/salsify/goldiloader/blob/master/lib/goldiloader/active_record_patches.rb#L166. I'll take a deeper look into this issue next week.