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
139139
140140 next if !should_include_attr? ( attribute_name , attr_data )
141141
142- unless relation . polymorphic?
142+ unless relation . nil? || ( relation . respond_to? ( : polymorphic?) && relation . polymorphic? )
143143 relation_class_name = ForestLiana . name_for ( relation . klass ) . demodulize
144144
145- if object . send ( relation . foreign_key . to_sym ) &&
145+ if object . respond_to? ( relation . foreign_key . to_sym ) &&
146146 @options [ :fields ] [ relation_class_name ] &.size == 1 &&
147147 @options [ :fields ] [ relation_class_name ] &.include? ( relation . klass . primary_key . to_sym )
148148
Original file line number Diff line number Diff line change @@ -56,10 +56,22 @@ def query_for_batch
5656
5757 def records
5858 records = @records . offset ( offset ) . limit ( limit ) . to_a
59-
6059 polymorphic_association , preload_loads = analyze_associations ( @resource )
60+
6161 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
6375 end
6476
6577 preload_cross_database_associations ( records , preload_loads )
You can’t perform that action at this time.
0 commit comments