Skip to content
/ pg_ltree Public
forked from sjke/pg_ltree

Rails gem for PostgreSQL lTree (pg_ltree)

License

Notifications You must be signed in to change notification settings

MProG/pg_ltree

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.0 (Sep 1, 2015)
License Released under the MIT license.

Gem Version Build Status Code Climate RubyDoc security

Installation

Add this line to your application's Gemfile:

gem 'pg_ltree'

And then execute:

$ bundle

Add ltree extension to PostgreSQL:

class AddLtreeExtension < ActiveRecord::Migration
  def change
    reversible do |dir|
      dir.up do
        execute <<-SQL
          CREATE EXTENSION IF NOT EXISTS ltree;
        SQL
      end
      dir.down do
        execute <<-SQL
          DROP EXTENSION IF NOT EXISTS ltree;
        SQL
      end
    end
  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)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 86.6%
  • HTML 10.6%
  • CSS 1.5%
  • JavaScript 1.3%