Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Ruby
  3. Ancestry

Ancestry

MITRuby

A Ruby gem for organizing ActiveRecord models into efficient tree structures using the materialized path pattern.

GitHubGitHub
3.9k stars471 forks0 contributors

What is Ancestry?

Ancestry is a Ruby gem that adds hierarchical tree functionality to ActiveRecord models. It solves the problem of organizing and querying nested data efficiently by using the materialized path pattern, storing parent-child relationships in a single database column.

Target Audience

Ruby on Rails developers who need to model hierarchical data (like categories, organizational charts, or nested comments) in their applications and want efficient tree operations without complex SQL.

Value Proposition

Developers choose Ancestry for its balance of performance and simplicity—it avoids extra tables, provides intuitive scopes for tree navigation, and supports real ActiveRecord associations, making it easier to integrate than raw SQL or other tree gems.

Overview

Organise ActiveRecord model into a tree structure

Use Cases

Best For

  • Building category hierarchies for e-commerce platforms
  • Modeling organizational charts or reporting structures
  • Implementing nested comment threads in forums or blogs
  • Creating file directory structures in web applications
  • Managing product taxonomies with multiple levels
  • Handling menu systems with nested items in Rails apps

Not Ideal For

  • Projects requiring hierarchical data with more than 900 levels of depth, as B-tree index constraints limit the ancestry column length.
  • Applications not built on Ruby on Rails or using a different ORM, since Ancestry is tightly integrated with ActiveRecord.
  • Systems needing real-time, high-concurrency updates to large trees, where the default :ruby update strategy could cause performance bottlenecks.
  • Teams preferring closure table patterns for complex relationship queries or write-heavy operations, as Ancestry's materialized path may not suit all use cases.

Pros & Cons

Pros

Efficient Tree Queries

Uses the materialized path pattern to enable single SQL queries for ancestors, descendants, and other tree relations, avoiding complex joins and improving performance.

Flexible Configuration Options

Supports multiple ancestry formats, orphan strategies, depth caching, and real ActiveRecord associations, allowing customization for different database and application needs.

Migration and Integrity Tools

Provides built-in methods like 'build_ancestry_from_parent_ids!' and 'check_ancestry_integrity!' for easy migration from other gems and ensuring data consistency without manual SQL.

ActiveRecord Integration

Implements relations as ActiveRecord scopes and offers optional real associations, making it seamless to use with Rails conventions like eager loading and joins.

Cons

Depth Limitation

Due to B-tree index constraints, the ancestry column can store only up to 2704 characters, limiting maximum tree depth to 900 nodes depending on ID length, as noted in the README.

Rails Version Dependencies

Ancestry has strict version compatibility with Rails (e.g., version 6.0 supports Rails 7.0 through 8.1), which can complicate upgrades and require careful version management.

Performance Trade-offs with Updates

The default :ruby update strategy triggers callbacks for each descendant when moving nodes, which may not scale well for large trees, though a :sql strategy is PostgreSQL-only.

Frequently Asked Questions

Quick Stats

Stars3,857
Forks471
Contributors0
Open Issues20
Last commit12 days ago
CreatedSince 2009

Tags

#hierarchical-data#orm#database#rails#tree-structure#data-modeling#ruby-gem#activerecord

Built With

R
Rails
R
Ruby
A
ActiveRecord

Included in

Ruby14.1k
Auto-fetched 1 day ago

Related Projects

PaperTrailPaperTrail

Track changes to your rails models

Stars6,996
Forks909
Last commit6 months ago
ActsAsTaggableOnActsAsTaggableOn

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.

Stars4,996
Forks1,193
Last commit1 month ago
AuditedAudited

Audited (formerly acts_as_audited) is an ORM extension that logs all changes to your Rails models.

Stars3,493
Forks663
Last commit5 months ago
public_activitypublic_activity

Easy activity tracking for models - similar to Github's Public Activity

Stars2,992
Forks331
Last commit3 months ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub