arel-ltree
Arel extension for PostgreSQL ltree type.
Installation
Add this line to your application's Gemfile:
gem 'arel-ltree'
And then execute:
$ bundle
Or install it yourself as:
$ gem install arel-ltree
Usage
Select all parent nodes:
Node.where(Node.arel_table[:path].ancestor_of('root.subtree.node')).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" @> 'root.subtree.node'::ltree;
Select all children nodes:
Node.where(Node.arel_table[:path].descendant_of('root.subtree')).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" <@ 'root.subtree'::ltree;
Match against ltree:
Node.where(Node.arel_table[:path].matches.ltree('root.subtree')).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" ~ 'root.subtree'::ltree;
ltree = Arel::Attributes::Ltree.new('root.subtree')
Node.where(Node.arel_table[:path].matches(ltree).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" ~ 'root.subtree'::ltree;
Match against lquery (simple regex for ltree):
Node.where(Node.arel_table[:path].matches.lquery('root.*{1}.node')).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" ~ 'root.*{1}.node'::lquery;
lquery = Arel::Attributes::Lquery.new('root.*{1}.node')
Node.where(Node.arel_table[:path].matches(lquery).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" ~ 'root.*{1}.node'::lquery;
Full-text match:
Node.where(Node.arel_table[:path].matches.ltxtquery('1 2')).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" @ '1 2';
lquery = Arel::Attributes::Ltxtquery.new('1 2')
Node.where(Node.arel_table[:path].matches(lquery).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" @ '1 2';
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request