VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 手册 > PostgreSQL手册 >
  • ltree 插件(2)

parent_id, :integer, index: true add_column :articles, :path, :ltree end end

执行rake db:migrate

在app/models/article.rb文件中添加下面那行。

class Article < ActiveRecord::Base
  has_ltree_hierarchy
end

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

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

parent_id存的是交节点的id,像上述所说的,path存的是以"."分隔的id。

除了rootchildrenparent,ltree_hierarchy还实现了其他查询方法。比如查叶子节点,查后代所有节点之类的。具体的查看官方的readme文件就好了。

完结。


相关教程