{"id":19136842,"url":"https://github.com/mdub/arboreal","last_synced_at":"2025-06-16T13:03:07.310Z","repository":{"id":649560,"uuid":"586458","full_name":"mdub/arboreal","owner":"mdub","description":"Efficient tree structures for ActiveRecord","archived":false,"fork":false,"pushed_at":"2022-04-28T17:27:48.000Z","size":274,"stargazers_count":10,"open_issues_count":2,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-19T14:57:36.271Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mdub.png","metadata":{"files":{"readme":"README.rdoc","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-03-30T12:29:43.000Z","updated_at":"2019-08-13T14:32:25.000Z","dependencies_parsed_at":"2022-08-07T06:00:01.120Z","dependency_job_id":null,"html_url":"https://github.com/mdub/arboreal","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdub%2Farboreal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdub%2Farboreal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdub%2Farboreal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdub%2Farboreal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mdub","download_url":"https://codeload.github.com/mdub/arboreal/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252761235,"owners_count":21800127,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T06:35:40.800Z","updated_at":"2025-05-06T20:12:47.144Z","avatar_url":"https://github.com/mdub.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"= Arboreal\n\nArboreal is yet another extension to ActiveRecord to support tree-shaped\ndata structures.\n\nArboreal surfaces relationships within the tree like +children+,\n+ancestors+, +descendants+, and +siblings+ as scopes, so that additional\nfiltering/pagination can be performed.\n \nIt delegates as much work as possible to the underlying DBMS, making it efficient to:\n\n* fetch all ancestors, descendants or siblings of a node\n* move nodes (or subtrees) around\n* prevent loops\n* rebuild the hierarchy\n\n== Getting started\n\nFirst, install the \"arboreal\" gem, and add it to your Rails project's \u003ctt\u003econfig/environment.rb\u003c/tt\u003e.  \n\nNext, you'll need a migration to add +parent_id+ and +ancestry_string+ columns, and indices:\n\n    class MakeThingsArboreal \u003c ActiveRecord::Migration\n\n      def self.up\n        add_column \"things\", \"parent_id\", :integer\n        add_index \"things\", [\"parent_id\"]\n        add_column \"things\", \"ancestry_string\", :string\n        add_index \"things\", [\"ancestry_string\"]\n      end\n\n      def self.down\n        remove_index \"things\", [\"ancestry_string\"]\n        remove_column \"things\", \"ancestry_string\"\n        remove_index \"things\", [\"parent_id\"]\n        remove_column \"things\", \"parent_id\"\n      end\n\n    end\n\nFinally, you can declare your model arboreal:\n\n    class Thing \u003c ActiveRecord::Base\n    \n      acts_arboreal\n      \n      # .. etc etc ...\n      \n    end\n\n== Navigating the tree\n\nArboreal adds the basic relationships you'd expect:\n\n* \u003ctt\u003eparent\u003c/tt\u003e\n* \u003ctt\u003echildren\u003c/tt\u003e\n\nIn addition, it provides the following handy methods on each tree-node:\n\n* \u003ctt\u003eancestors\u003c/tt\u003e\n* \u003ctt\u003edescendants\u003c/tt\u003e\n* \u003ctt\u003esubtree\u003c/tt\u003e (the node itself, plus descendants)\n* \u003ctt\u003esiblings\u003c/tt\u003e\n* \u003ctt\u003eroot\u003c/tt\u003e (the topmost ancestor)\n\nThe first four return scopes, to which additional filtering, ordering or limits may be applied.\n\nAt the class-level:\n\n* \u003ctt\u003eroots\u003c/tt\u003e is a named-scope returning all the nodes without parents\n* \u003ctt\u003erebuild_ancestry\u003c/tt\u003e rebuilds the ancestry cache, as described below\n\n== Rebuilding the ancestry cache\n \nInternally, Arboreal uses the +ancestry_string+ column to cache the path down the tree to each node (or more correctly, it's parent.  This technique - a variant of \"path enumeration\" or \"materialized paths\" - allows efficient retrieval of both ancestors and descendants.\n\nIt's conceivable that the computed ancestry-string values may get out of whack, particularly if changes are made directly to the database.  If you suspect corruption, you can restore sanity using \u003ctt\u003erebuild_ancestry\u003c/tt\u003e, e.g\n\n    Thing.rebuild_ancestry\n\nThe ancestry rebuild is implemented in SQL to leverage the underlying DBMS, and so is pretty efficient, even on large trees.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdub%2Farboreal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmdub%2Farboreal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdub%2Farboreal/lists"}