@@ -297,30 +297,41 @@ def compute_select_fields
297297 select << "#{ @resource . table_name } .#{ pk } "
298298 end
299299
300+ # Handle ActiveStorage associations from both @includes and @field_names_requested
301+ active_storage_associations_processed = Set . new
302+
303+ ( @includes + @field_names_requested ) . each do |path |
304+ association = path . is_a? ( Symbol ) ? @resource . reflect_on_association ( path ) : get_one_association ( path )
305+ next unless association
306+ next if active_storage_associations_processed . include? ( association . name )
307+ next unless is_active_storage_association? ( association )
308+
309+ # Include all columns from ActiveStorage tables to avoid initialization errors
310+ table_name = association . table_name
311+ association . klass . column_names . each do |column_name |
312+ select << "#{ table_name } .#{ column_name } "
313+ end
314+
315+ # Include the foreign key from the main resource (e.g., blob_id, record_id)
316+ if association . macro == :belongs_to || association . macro == :has_one
317+ foreign_keys = Array ( association . foreign_key )
318+ foreign_keys . each do |fk |
319+ select << "#{ @resource . table_name } .#{ fk } "
320+ end
321+ end
322+
323+ active_storage_associations_processed . add ( association . name )
324+ end
325+
300326 @field_names_requested . each do |path |
301327 association = get_one_association ( path )
302328 if association
303329 while association . options [ :through ]
304330 association = get_one_association ( association . options [ :through ] )
305331 end
306332
307- # For ActiveStorage associations, include all required columns
308- if is_active_storage_association? ( association )
309- # Include all columns from ActiveStorage tables to avoid initialization errors
310- table_name = association . table_name
311- association . klass . column_names . each do |column_name |
312- select << "#{ table_name } .#{ column_name } "
313- end
314-
315- # Also include the foreign key from the main resource (e.g., blob_id, record_id)
316- if association . macro == :belongs_to || association . macro == :has_one
317- foreign_keys = Array ( association . foreign_key )
318- foreign_keys . each do |fk |
319- select << "#{ @resource . table_name } .#{ fk } "
320- end
321- end
322- next
323- end
333+ # Skip ActiveStorage associations - already processed above
334+ next if is_active_storage_association? ( association )
324335
325336 if SchemaUtils . polymorphic? ( association )
326337 select << "#{ @resource . table_name } .#{ association . foreign_type } "
0 commit comments