Skip to content

Allow ancestry to define a primary ancestry key#747

Open
kbrock wants to merge 1 commit intostefankroes:masterfrom
kbrock:primary_ancestry_key
Open

Allow ancestry to define a primary ancestry key#747
kbrock wants to merge 1 commit intostefankroes:masterfrom
kbrock:primary_ancestry_key

Conversation

@kbrock
Copy link
Copy Markdown
Collaborator

@kbrock kbrock commented Mar 28, 2026

Description

Not sure if I want to commit, but putting this out there.

Many of the concepts are good. But not as readable as I'd like.
A lot of change, so this will get stale quickly.

Fixes #698

This allows ancestry to have a/b/c/ but the primary keys be 1,2,3

Before

After

Type of Change

  • Feature

Checklist

  • My code follows the style guidelines (e.g., RuboCop)
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally (bundle exec rake test)

How Has This Been Tested?

@kbrock kbrock force-pushed the primary_ancestry_key branch 3 times, most recently from 57bf3c5 to 2c0362a Compare March 30, 2026 03:38
Introduce primary_ancestry_key class variable and ancestry_id instance
method as the indirection point for which column values are stored in
ancestry paths. Defaults to :id. On Rails 7.2+, auto-detects from
primary_key. On older Rails, must be passed explicitly via
has_ancestry primary_key: :code.

Replaces bare id/node.id with ancestry_id/node.ancestry_id throughout
static modules (instance_methods.rb, class_methods.rb) and builder.
Replaces scope.primary_key with scope.primary_ancestry_key for record
lookups by ancestry values.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@kbrock kbrock force-pushed the primary_ancestry_key branch from 2c0362a to 3eb79fa Compare March 31, 2026 03:56
@kbrock
Copy link
Copy Markdown
Collaborator Author

kbrock commented Mar 31, 2026

@a5-stable does this look like it would meet your needs?

I'm trying to see if there is a way to patch this in or changes I can make in core so it is easier to patch.

Have gotten this to be less invasive, but it sure goes everywhere. I was hoping that I could localize the changes just to the builder. Haven't gotten there yet and a little stuck.

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.

1 participant