ams_lazy_relationships icon indicating copy to clipboard operation
ams_lazy_relationships copied to clipboard

Rails 7 Support

Open StevenElberger opened this issue 1 year ago • 2 comments

It looks like the public interface for ActiveRecord::Associations::Preloader has changed in Rails 7 and is causing a problem in AmsLazyRelationships::Loaders::Association#load_data:

def load_data(records, loader)
  # This is not the correct interface anymore!
  ::ActiveRecord::Associations::Preloader.new.preload(
    records_to_preload(records), association_name
  )
  ...
end

My gemfile has: gem 'rails', '~> 7.0.3'

See: https://github.com/rails/rails/blob/04972d9b9ef60796dc8f0917817b5392d61fcf09/activerecord/lib/active_record/associations/preloader.rb#L96

When trying to use this gem, I see the following error:

{
  "errors": [
    {
      "status": 500,
      "detail": "missing keywords: :records, :associations",
      "trace": [
        "/Users/stevenelberger/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/activerecord-7.0.3.1/lib/active_record/associations/preloader.rb:96:in `initialize'",
        "/Users/stevenelberger/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ams_lazy_relationships-0.3.2/lib/ams_lazy_relationships/loaders/association.rb:25:in `new'",
        "/Users/stevenelberger/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ams_lazy_relationships-0.3.2/lib/ams_lazy_relationships/loaders/association.rb:25:in `load_data'",
        "/Users/stevenelberger/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ams_lazy_relationships-0.3.2/lib/ams_lazy_relationships/loaders/base.rb:21:in `block in load'",
        "/Users/stevenelberger/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/batch-loader-2.0.1/lib/batch_loader.rb:92:in `__ensure_batched'",
        "/Users/stevenelberger/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/batch-loader-2.0.1/lib/batch_loader.rb:53:in `__sync'",
        "/Users/stevenelberger/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ams_lazy_relationships-0.3.2/lib/ams_lazy_relationships/core/evaluation.rb:29:in `load_lazy_relationship'",
        "/Users/stevenelberger/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ams_lazy_relationships-0.3.2/lib/ams_lazy_relationships/core/lazy_relationship_method.rb:42:in `block in lazy_relationship'",
        "/Users/stevenelberger/Documents/GitHub/my-app/app/serializers/word_serializer.rb:14:in `block (2 levels) in \u003cclass:WordSerializer\u003e'",
        "/Users/stevenelberger/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ams_lazy_relationships-0.3.2/lib/ams_lazy_relationships/extensions/reflection.rb:63:in `value'"
      ]
    }
  ]
}

Are there any plans to update this gem to support the new interface?

StevenElberger avatar Jul 31 '22 22:07 StevenElberger

@StevenElberger I'll be at this issue soon I bet ) In meanwhile we are open for incoming PRs if you like to fix it urgently )

stokarenko avatar Aug 03 '22 10:08 stokarenko

Unfortunately, the ::ActiveRecord::Associations::Preloader class uses :nodoc: ( https://github.com/rails/rails/blob/v7.0.3.1/activerecord/lib/active_record/associations/preloader.rb#L44 ) which as per: https://guides.rubyonrails.org/api_documentation_guidelines.html#method-visibility means that it's an internal API and isn't meant to be used by us Rails users.

Since more and more people are upgrading applications to Rails 7 and/or starting new applications on Rails 7 I agree that it would be good if we could fix this so that the code works on Rails 7 without using an internal Rails API. Unfortunately I don't know how offhand (possibly via the https://github.com/exAspArk/batch-loader gem?). Hopefully others like @stokarenko do.

scifisamurai avatar Aug 11 '22 19:08 scifisamurai

Any updates on this ?

Natblow avatar Mar 10 '23 18:03 Natblow

v0.4.0 pushed! Sorry for such a long delay people 🙈

Bajena avatar Nov 18 '23 14:11 Bajena