chrisortman / pg_ltree

Rails gem for PostgreSQL lTree (pg_ltree)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PgLtree

Gem that allows use ltree in ActiveRecord models.

It uses a implementation based around PostgreSQL's ltree data type, associated functions and operators.

Author Andrei Panamarenka
Version 1.1.8 (December 13, 2019)
License Released under the MIT license.

Gem Version Build Status Code Climate RubyDoc

Support

  • Ruby 2.*
  • Rails >= 4, < 7
  • Pg adapter (gem 'pg') >= 0.17, < 2

Installation

Add this line to your application's Gemfile:

gem 'pg_ltree', '1.1.8'

And then execute:

$ bundle

Add ltree extension to PostgreSQL:

class AddLtreeExtension < ActiveRecord::Migration
  def change
    enable_extension 'ltree'
  end
end

Update your model:

class AnyModel < ActiveRecord::Migration
  def change
    add_column :any_model, :path, :ltree

    add_index :any_model, :path, using: :gist
  end
end

Run migrations:

$ bundle exec rake db:migrate

Usage

  class AnyModel < ActiveRecord::Base
    ltree :path
    # ltree :path, cascade: false # Disable cascade update and delete
  end

  root     = AnyModel.create!(path: 'Top')
  child    = AnyModel.create!(path: 'Top.Science')
  subchild = AnyModel.create!(path: 'Top.Science.Astronomy')

  root.parent   # => nil
  child.parent # => root
  root.children # => [child]
  root.children.first.children.first # => subchild
  subchild.root # => root

For find a lots of additional information about PgLtee see:

About

Rails gem for PostgreSQL lTree (pg_ltree)

License:MIT License


Languages

Language:Ruby 100.0%