Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
fire2000 opened this issue Jan 12, 2017 · 1 comment · Fixed by #40

Comments

@fire2000
Copy link

fire2000 commented Jan 12, 2017

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.

@jturkel
Copy link
Member

jturkel commented Jan 13, 2017

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.

jturkel added a commit that referenced this issue Jan 17, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants