From 5056c992490cfa1704e90e00f8068e78a69531f1 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 22 Jul 2025 10:36:19 +0200 Subject: [PATCH 1/2] fix: polymorphic associations by including all belongs_to foreign keys --- lib/forest_liana/active_record_override.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/forest_liana/active_record_override.rb b/lib/forest_liana/active_record_override.rb index 08e53c06..ef6810ec 100644 --- a/lib/forest_liana/active_record_override.rb +++ b/lib/forest_liana/active_record_override.rb @@ -33,7 +33,7 @@ def apply_column_aliases(relation) keys = [join_part.base_klass.primary_key] # Always include the primary key # # %%% Optional to include all foreign keys: - # keys.concat(join_part.base_klass.reflect_on_all_associations.select { |a| a.belongs_to? }.map(&:foreign_key)) + keys.concat(join_part.base_klass.reflect_on_all_associations.select { |a| a.belongs_to? }.map(&:foreign_key)) # Add foreign keys out to referenced tables that we belongs_to join_part.children.each { |child| keys << child.reflection.foreign_key if child.reflection.belongs_to? } From f53a5bf28ab019422dd5cc07ab531f8d3f697401 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 22 Jul 2025 10:47:15 +0200 Subject: [PATCH 2/2] fix: uniq keys --- lib/forest_liana/active_record_override.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/forest_liana/active_record_override.rb b/lib/forest_liana/active_record_override.rb index ef6810ec..e23a9a32 100644 --- a/lib/forest_liana/active_record_override.rb +++ b/lib/forest_liana/active_record_override.rb @@ -33,7 +33,7 @@ def apply_column_aliases(relation) keys = [join_part.base_klass.primary_key] # Always include the primary key # # %%% Optional to include all foreign keys: - keys.concat(join_part.base_klass.reflect_on_all_associations.select { |a| a.belongs_to? }.map(&:foreign_key)) + keys.concat(join_part.base_klass.reflect_on_all_associations.select { |a| a.belongs_to? }.map(&:foreign_key)).compact.uniq # Add foreign keys out to referenced tables that we belongs_to join_part.children.each { |child| keys << child.reflection.foreign_key if child.reflection.belongs_to? }