crashtech/torque-postgresql

[rails 7] preloader bug: "private method `load_records' called for"

Closed this issue ยท 7 comments

Hi,

When running torque-postgresql with rails 7 I get this error when I am using the preloader directly:

Reproduce script:

# frozen_string_literal: true

require "bundler/inline"

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", "~> 7.0.0"
  gem "pg"
  gem "torque-postgresql"
end

require "active_record"
require "logger"
require 'torque-postgresql'

ActiveRecord::Base.establish_connection(
  adapter:  "postgresql",
  database: "test",
  encoding: "unicode",
  host:     "localhost",
  port:     "5432",
  password: "12345",
  username: "test")

ActiveRecord::Schema.define do
  drop_table "employees", if_exists: true
  drop_table "projects", if_exists: true

  create_table :comments, force: true do |t|
    t.integer :author_id
  end

  create_table :authors, force: true do |t|
  end
end

class Author < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :author
end

author = Author.create!
author.comments << [Comment.new, Comment.new]

ActiveRecord::Associations::Preloader.new(records: Author.all, associations: [:comments]).call

Error:

Traceback (most recent call last):
        8: from bug.rb:52:in `<main>'
        7: from /Users/yosi/.rvm/gems/ruby-2.7.2/gems/activerecord-7.0.2.2/lib/active_record/associations/preloader.rb:118:in `call'
        6: from /Users/yosi/.rvm/gems/ruby-2.7.2/gems/activerecord-7.0.2.2/lib/active_record/associations/preloader/batch.rb:27:in `call'
        5: from /Users/yosi/.rvm/gems/ruby-2.7.2/gems/activerecord-7.0.2.2/lib/active_record/associations/preloader/batch.rb:41:in `group_and_load_similar'
        4: from /Users/yosi/.rvm/gems/ruby-2.7.2/gems/activerecord-7.0.2.2/lib/active_record/associations/preloader/batch.rb:41:in `each_pair'
        3: from /Users/yosi/.rvm/gems/ruby-2.7.2/gems/activerecord-7.0.2.2/lib/active_record/associations/preloader/batch.rb:42:in `block in group_and_load_similar'
        2: from /Users/yosi/.rvm/gems/ruby-2.7.2/gems/activerecord-7.0.2.2/lib/active_record/associations/preloader/association.rb:32:in `load_records_in_batch'
        1: from /Users/yosi/.rvm/gems/ruby-2.7.2/gems/activerecord-7.0.2.2/lib/active_record/associations/preloader/association.rb:32:in `each'
/Users/yosi/.rvm/gems/ruby-2.7.2/gems/activerecord-7.0.2.2/lib/active_record/associations/preloader/association.rb:33:in `block in load_records_in_batch': private method `load_records' called for #<ActiveRecord::Associations::Preloader::Association:0x00000001503081e8> (NoMethodError)
Did you mean?  loaded?

When I comment out require & gem of torque it works as expected.

Did some "dirty" commit in my fork - yosiat@9c5e681 and it works with it

Hi @yosiat (cc @crashtech) thanks for this! I am also looking for a ways to make this lib compatible with Rails 7. See my PR #65.

Would you like make a PR with your fix (even if "dirty") based on mine?

Would be great to get this all solved, it starts to be a problem that this gem is not compat. with Rails 7.

Hi @tomasc

The fix I did is not working 100% , we have in our CI lots of failures from -

#<NoMethodError: undefined method `already_loaded?' for #<ActiveRecord::Associations::Preloader::Association:0x00007fbf24860200>> with backtrace:

         # ./vendor/bundle/ruby/2.7.0/bundler/gems/torque-postgresql-9c5e68147a8b/lib/torque/postgresql/associations/preloader/association.rb:50:in `run_array_for_belongs_to_many'

         # ./vendor/bundle/ruby/2.7.0/bundler/gems/torque-postgresql-9c5e68147a8b/lib/torque/postgresql/associations/preloader/association.rb:15:in `run'

         # ./vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/associations/preloader/association.rb:34:in `block in load_records_in_batch'

         # ./vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/associations/preloader/association.rb:32:in `each'

         # ./vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/associations/preloader/association.rb:32:in `load_records_in_batch'

         # ./vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/associations/preloader/batch.rb:42:in `block in group_and_load_similar'

         # ./vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/associations/preloader/batch.rb:41:in `each_pair'

         # ./vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/associations/preloader/batch.rb:41:in `group_and_load_similar'

         # ./vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/associations/preloader/batch.rb:27:in `call'

         # ./vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/associations/preloader.rb:118:in `call'

I need to investigate this more and see what's causing the issue here, anyway I prefer @crashtech to give his opinion here.

update: managed to fix the issue above (with already_loaded) and issue in original posts - master...yosiat:rails7-preloader2

currently my app CI is failing on timestamps comparison (didn't checked if it's related to rails/torque)

Version 3.0.0 is released now!

Thanks Carlos, amazing!!

Thanks @crashtech this is so amazing!