0.09
No commit activity in last 3 years
No release in over 3 years
There's a lot of open issues
Organizes ActiveRecord models into a tree/hierarchy using a materialized path implementation based around PostgreSQL's ltree datatype. ltree's operators ensure that queries are fast and easily understood.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0

Runtime

>= 5.2.0
~> 1.1.0
 Project Readme

Gem Version Build Status

Ltree Hierarchy

A simplistic gem that allows ActiveRecord models to be organized in a tree or hierarchy. It uses a materialized path implementation based around PostgreSQL's ltree data type, associated functions and operators.

Why might you want to use it?

  • You want to be able to construct optimized hierarchical queries with ease, both from Ruby AND raw SQL.
  • You want to be able to compose complex arel expressions from pre-defined building blocks.
  • You prefer PostgreSQL over other relational DBs.

Installation

Add this line to your application's Gemfile:

gem 'ltree_hierarchy'

And then execute:

$ bundle

Add ltree extension to PostgreSQL:

$ psql -U postgres -d my_database
-> CREATE EXTENSION IF NOT EXISTS ltree;

Update your table:

class AddLtreeToLocations < ActiveRecord::Migration
  def self.up
    add_column :locations, :parent_id, :integer
    add_column :locations, :path, :ltree

    add_index :locations, :parent_id
  end

  def self.down
    remove_index :locations, :parent_id
    remove_column :locations, :parent_id
    remove_column :locations, :path
  end
end

Run migrations:

$ bundle exec rake db:migrate

Usage

  class Location < ActiveRecord::Base
    has_ltree_hierarchy
  end

  root     = Location.create!(name: 'UK')
  child    = Location.create!(name: 'London', parent: root)
  subchild = Location.create!(name: 'Hackney', parent: child)

  root.parent   # => nil
  child.parent # => root
  root.children # => [child]
  root.children.first.children.first # => subchild
  subchild.root # => root
  • .roots
  • .leaves
  • .at_depth(n)
  • .lowest_common_ancestors(scope)
  • #parent
  • #ancestors
  • #self_and_ancestors
  • #siblings
  • #self_and_siblings
  • #children
  • #self_and_children
  • #descendants
  • #self_and_descendants
  • #leaves

TODO

  • Better error message for circular references.
  • Don't neglect i18n.