File tree Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -139,10 +139,10 @@ def has_one_relationships
139
139
140
140
next if !should_include_attr? ( attribute_name , attr_data )
141
141
142
- unless relation . polymorphic?
142
+ unless relation . nil? || ( relation . respond_to? ( : polymorphic?) && relation . polymorphic? )
143
143
relation_class_name = ForestLiana . name_for ( relation . klass ) . demodulize
144
144
145
- if object . send ( relation . foreign_key . to_sym ) &&
145
+ if object . respond_to? ( relation . foreign_key . to_sym ) &&
146
146
@options [ :fields ] [ relation_class_name ] &.size == 1 &&
147
147
@options [ :fields ] [ relation_class_name ] &.include? ( relation . klass . primary_key . to_sym )
148
148
Original file line number Diff line number Diff line change @@ -56,10 +56,22 @@ def query_for_batch
56
56
57
57
def records
58
58
records = @records . offset ( offset ) . limit ( limit ) . to_a
59
-
60
59
polymorphic_association , preload_loads = analyze_associations ( @resource )
60
+
61
61
if polymorphic_association && Rails ::VERSION ::MAJOR >= 7
62
- # TODO
62
+ preloader = ActiveRecord ::Associations ::Preloader . new ( records : records , associations : polymorphic_association )
63
+ preloader . loaders
64
+ preloader . branches . each do |branch |
65
+ branch . loaders . each do |loader |
66
+ records_by_owner = loader . records_by_owner
67
+ records_by_owner . each do |record , association |
68
+ record_index = records . find_index { |r | r . id == record . id }
69
+ records [ record_index ] . define_singleton_method ( branch . association ) do
70
+ association . first
71
+ end
72
+ end
73
+ end
74
+ end
63
75
end
64
76
65
77
preload_cross_database_associations ( records , preload_loads )
You can’t perform that action at this time.
0 commit comments