Skip to content

Commit 15ab41b

Browse files
Dogan AYmatthv
authored andcommitted
fix: csv issues
1 parent 18a8083 commit 15ab41b

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

app/services/forest_liana/resources_getter.rb

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,21 @@ def compute_select_fields
297297
select << "#{@resource.table_name}.#{pk}"
298298
end
299299

300+
# Include columns used in default ordering for batch cursor compatibility
301+
if @resource.respond_to?(:default_scoped) && @resource.default_scoped.order_values.any?
302+
@resource.default_scoped.order_values.each do |order_value|
303+
if order_value.is_a?(Arel::Nodes::Ordering)
304+
# Extract column name from Arel node
305+
column_name = order_value.expr.name if order_value.expr.respond_to?(:name)
306+
select << "#{@resource.table_name}.#{column_name}" if column_name
307+
elsif order_value.is_a?(String) || order_value.is_a?(Symbol)
308+
# Handle simple column names
309+
column_name = order_value.to_s.split(' ').first.split('.').last
310+
select << "#{@resource.table_name}.#{column_name}"
311+
end
312+
end
313+
end
314+
300315
# Handle ActiveStorage associations from both @includes and @field_names_requested
301316
active_storage_associations_processed = Set.new
302317

@@ -326,21 +341,39 @@ def compute_select_fields
326341
@field_names_requested.each do |path|
327342
association = get_one_association(path)
328343
if association
344+
# Handle :through associations - resolve to the direct association
345+
original_association = association
346+
through_chain = []
329347
while association.options[:through]
348+
through_chain << association.options[:through]
330349
association = get_one_association(association.options[:through])
331350
end
332351

333352
# Skip ActiveStorage associations - already processed above
334353
next if is_active_storage_association?(association)
335354

336-
if SchemaUtils.polymorphic?(association)
337-
select << "#{@resource.table_name}.#{association.foreign_type}"
338-
end
355+
# For :through associations, only add foreign keys from the direct (first) association in the chain
356+
# Don't try to select columns from the main table for the final :through target
357+
if through_chain.any?
358+
# Use the first association in the through chain
359+
first_through = get_one_association(through_chain.first)
360+
if first_through && (first_through.macro == :belongs_to || first_through.macro == :has_one)
361+
foreign_keys = Array(first_through.foreign_key)
362+
foreign_keys.each do |fk|
363+
select << "#{@resource.table_name}.#{fk}"
364+
end
365+
end
366+
else
367+
# Direct association (not :through)
368+
if SchemaUtils.polymorphic?(association)
369+
select << "#{@resource.table_name}.#{association.foreign_type}"
370+
end
339371

340-
if association.macro == :belongs_to || association.macro == :has_one
341-
foreign_keys = Array(association.foreign_key)
342-
foreign_keys.each do |fk|
343-
select << "#{@resource.table_name}.#{fk}"
372+
if association.macro == :belongs_to || association.macro == :has_one
373+
foreign_keys = Array(association.foreign_key)
374+
foreign_keys.each do |fk|
375+
select << "#{@resource.table_name}.#{fk}"
376+
end
344377
end
345378
end
346379
end

0 commit comments

Comments
 (0)