Skip to content

Allow ancestry to define a primary ancestry key - #747

Open
kbrock wants to merge 1 commit into
stefankroes:masterfrom
kbrock:primary_ancestry_key
Open

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

Conversation

@kbrock

@kbrock kbrock commented Mar 28, 2026

Copy link
Copy Markdown
Collaborator

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 4 times, most recently from 2c0362a to 3eb79fa Compare March 31, 2026 03:56
@kbrock

kbrock commented Mar 31, 2026

Copy link
Copy Markdown
Collaborator Author

@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.

@a5-stable

Copy link
Copy Markdown
Contributor

@kbrock Hi, thanks for creating the PR! I tried running our product with this branch.
Ancestry works well and It seems to satisfy our requirements.
Is there anything I can do to help move this forward?

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) <noreply@anthropic.com>
@kbrock
kbrock force-pushed the primary_ancestry_key branch from 3eb79fa to 97705f1 Compare April 15, 2026 07:44
@kbrock

kbrock commented Apr 16, 2026

Copy link
Copy Markdown
Collaborator Author

Overview

I feel I am missing something.

I don't understand how this code can work without a primary id.

The existing STI support requires unscoped access, which will likely cause your plugin to fail.

My concern is I don't want to introduce a lot more complexity than I will support, and others will have to debug if I'm not sure it will benefit you.

Please help me understand how you associate 2 classes together and how the controller tells the model (and database) which record to return when you request :user_id=>1.

Request

Do you have a simple example of a code base?

  • 2-3(?) models.
  • One has a hierarchy (that requires this branch).
  • CRUD controllers/scaffolding with a show page.
  • A view that displays the model/relationship at different time intervals.

What I hope to gain

I want to look at the plugin/gem's use of default scopes or how the date is determined.

I want to see how you pass the data to the database so I can ensure the way Ancestry turns off scopes does not affect you.

Also, it seems like all records are read-only. Not sure how ancestry would handle this.

Thank you for your assistance.

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.

2 participants