Skip to content

Commit 3ee33e1

Browse files
authored
Merge pull request #62 from clio/fix_active_record_7_nil_class_problem
Remove the association loader if the polymorphic class is not a child of ActiveRecord::Base
2 parents c52acf4 + 4950ad8 commit 3ee33e1

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/jit_preloader.rb

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
require 'jit_preloader/active_record/associations/singular_association'
1111
if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("7.0.0")
1212
require 'jit_preloader/active_record/associations/preloader/ar7_association'
13+
require 'jit_preloader/active_record/associations/preloader/ar7_branch'
1314
elsif Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("6.0.0")
1415
require 'jit_preloader/active_record/associations/preloader/ar6_association'
1516
elsif Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("5.2.2")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module JitPreloader
2+
module PreloaderBranch
3+
"""
4+
ActiveRecord version >= 7.x.x introduced an improvement for preloading associations in batches:
5+
https://github.com/rails/rails/blob/main/activerecord/lib/active_record/associations/preloader.rb#L121
6+
7+
Our existing monkey-patches will ignore associations whose classes are not descendants of
8+
ActiveRecord::Base (example: https://github.com/clio/jit_preloader/blob/master/lib/jit_preloader/active_record/associations/preloader/ar6_association.rb#L19).
9+
But this change breaks that behaviour because now Batch is calling `klass.base_class` (a method defined by ActiveRecord::Base)
10+
before we have a chance to filter out the non-AR classes.
11+
This patch for AR 7.x makes the Branch class ignore any association loaders that aren't for ActiveRecord::Base subclasses.
12+
"""
13+
14+
def loaders
15+
@loaders = super.find_all do |loader|
16+
loader.klass < ::ActiveRecord::Base
17+
end
18+
end
19+
end
20+
end
21+
22+
ActiveRecord::Associations::Preloader::Branch.prepend(JitPreloader::PreloaderBranch)

0 commit comments

Comments
 (0)