diff --git a/lib/closure_tree/support.rb b/lib/closure_tree/support.rb index 3a0a8ae..1b21739 100644 --- a/lib/closure_tree/support.rb +++ b/lib/closure_tree/support.rb @@ -36,6 +36,10 @@ def hierarchy_class_for_model # Rails 8.1+ requires an implicit_order_column for models without a primary key self.implicit_order_column = 'ancestor_id' + # Rails uses the primary key to correctly match associations when using a join to preload (e.g. via `eager_load`). + # The migration generator adds a unique index across these three columns so this is safe. + self.primary_key = [:ancestor_id, :descendant_id, :generations] + belongs_to :ancestor, class_name: model_class_name belongs_to :descendant, class_name: model_class_name def ==(other)