@@ -58,7 +58,7 @@ def records
5858 records = @records . offset ( offset ) . limit ( limit ) . to_a
5959 polymorphic_association , preload_loads = analyze_associations ( @resource )
6060
61- if polymorphic_association && Rails ::VERSION ::MAJOR >= 7
61+ if polymorphic_association . any? && Rails ::VERSION ::MAJOR >= 7
6262 preloader = ActiveRecord ::Associations ::Preloader . new ( records : records , associations : polymorphic_association )
6363 preloader . loaders
6464 preloader . branches . each do |branch |
@@ -85,7 +85,6 @@ def preload_cross_database_associations(records, preload_loads)
8585 next unless separate_database? ( @resource , association )
8686
8787 columns = columns_for_cross_database_association ( association_name )
88-
8988 if association . macro == :belongs_to
9089 foreign_key = association . foreign_key
9190 primary_key = association . klass . primary_key
@@ -125,27 +124,22 @@ def preload_cross_database_associations(records, preload_loads)
125124 end
126125
127126 def columns_for_cross_database_association ( association_name )
128- return [ :id ] unless @params [ :fields ] . present?
129-
130- fields = @params [ :fields ] [ association_name . to_s ]
131- return [ :id ] unless fields
132-
133- base_fields = fields . split ( ',' ) . map ( &:strip ) . map ( &:to_sym ) | [ :id ]
134-
135127 association = @resource . reflect_on_association ( association_name )
136- extra_key = association . foreign_key
137128
138- # Add the foreign key used for the association to ensure it's available in the preloaded records
139- # This is necessary for has_one associations, without it calling record.public_send(foreign_key) would raise a "missing attribute" error
140- base_fields << extra_key if association . macro == :has_one
129+ # Always include all columns of the associated model to avoid missing attribute errors
130+ columns = association . klass . column_names . map ( &:to_sym )
131+
132+ # Ensure the foreign key is present for manual binding (especially for has_one)
133+ columns << association . foreign_key . to_sym if association . macro == :has_one
141134
142- base_fields . uniq
135+ columns . uniq
143136 end
144137
145138 def compute_includes
146139 associations_has_one = ForestLiana ::QueryHelper . get_one_associations ( @resource )
140+
147141 @optional_includes = [ ]
148- if @field_names_requested
142+ if @field_names_requested && @params [ 'searchExtended' ] . to_i != 1
149143 includes = associations_has_one . map do |association |
150144 association_name = association . name . to_s
151145
@@ -175,7 +169,10 @@ def compute_includes
175169
176170 @includes = ( includes & @field_names_requested ) . concat ( includes_for_smart_search )
177171 else
178- @includes = associations_has_one . map ( &:name )
172+ @includes = associations_has_one
173+ # Avoid eager loading has_one associations pointing to a different database as ORM can't join cross databases
174+ . reject { |association | separate_database? ( @resource , association ) }
175+ . map ( &:name )
179176 end
180177 end
181178
0 commit comments