Skip to content

Commit 8c7e490

Browse files
fix: eager loading of hierarchies by defining the primary key (#465)
1 parent 87be762 commit 8c7e490

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

lib/closure_tree/support.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def hierarchy_class_for_model
4343
# Rails 8.1+ requires an implicit_order_column for models without a primary key
4444
self.implicit_order_column = 'ancestor_id'
4545

46+
# Rails uses the primary key to correctly match associations when using a join to preload (e.g. via `eager_load`).
47+
# The migration generator adds a unique index across these three columns so this is safe.
48+
self.primary_key = [:ancestor_id, :descendant_id, :generations]
49+
4650
belongs_to :ancestor, class_name: model_class_name
4751
belongs_to :descendant, class_name: model_class_name
4852
def ==(other)

0 commit comments

Comments
 (0)