Skip to content

Allow customization of column for ancestry relationships (using non-primary key) #695

@a5-stable

Description

@a5-stable

Greetings,

The ancestry gem currently assumes that the parent-child relationship is managed using the primary key.
However, there are certain cases where a different unique column needs to be used to manage the relationship when we use other columns as a identifier.

This is how it would behave;

class Category
   has_ancestry ancestry_identifier_column: :another_identifier_column
end

root = Category.create!(another_identifier_column: 100)
node = Category.new(another_identifier_column: 200)
node.ancestor_ids = [root.another_identifier_column]
node.save!

node.ancestry
#=> 100

Some gems allow customizing the column used for managing parent-child relationships. For instance, the awesome_nested_set gem provides a primary_column option that enables users to specify which column should be used as the parent reference (parent_id).

I am currently working on a PR to add this feature to ancestry, as I need it for my product. I’d be happy to submit it if you find this approach acceptable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions