Skip to content

fix: allow preloading self_and_ancestors and self_and_descendants - #499

Open
yskttm wants to merge 2 commits into
ClosureTree:masterfrom
yskttm:fix/preload-self-and-ancestors
Open

yskttm wants to merge 2 commits into
ClosureTree:masterfrom
yskttm:fix/preload-self-and-ancestors

Conversation

@yskttm

@yskttm yskttm commented Dec 2, 2026

Copy link
Copy Markdown

Fixes #137

What this fixes

Preloading self_and_ancestors fails on every adapter:

Tag.includes(:self_and_ancestors).to_a
# ActiveRecord::StatementInvalid: Unknown column 'tag_hierarchies.generations' in 'order clause'

self_and_descendants fails the same way on models without an order: option.

Why

Both associations order by <hierarchy>.generations. A lazy load joins the hierarchy table, so the order works. A preload loads the through records first and then runs SELECT tags.* WHERE id IN (...), which doesn't join the hierarchy table, so the order is invalid there.

The fix

Drop the order from self_and_ancestors, and from self_and_descendants when there's no order: option.

The through associations (ancestor_hierarchies and descendant_hierarchies) already order by generations. Rails applies that order when it loads lazily, and keeps the through records' order when it preloads, so the results are still ordered by generations.

self_and_descendants with an order: option keeps its order. It needs generations first and then the order column, which the through association's order can't express. That case still can't be preloaded.

Testing

  • Added test/closure_tree/preload_test.rb: preload and includes of self_and_ancestors on the PostgreSQL, MySQL and SQLite tag models, plus self_and_descendants on the adoptable tag models. Checks the order and ancestry_path. All 18 tests error on master and pass with the fix.
  • The full suite passes on all three databases (387 runs, 0 failures).
  • The lazy-load SQL for both associations is the same before and after the change.

The association scopes ordered by the hierarchy table's generations column,
but the preloader loads the source records in a query that doesn't join that
table, so preload/includes raised ActiveRecord::StatementInvalid
(PostgreSQL: missing FROM-clause entry; SQLite: no such column).

The through associations already order by generations. Rails applies that
order when loading lazily and keeps the through-record order when preloading,
so the extra order is dropped from self_and_ancestors, and from
self_and_descendants when no order option is set. Lazy SQL is unchanged.

Refs ClosureTree#137, ClosureTree#70
@yskttm
yskttm marked this pull request as ready for review December 2, 2026 06:34

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't eager load self_and_ancestors association in Rails 4.1

1 participant