SlideShare a Scribd company logo
1 of 18
Download to read offline
• acts as list
• acts as tree
• acts as nested set
• better nested set
acts as list




• Rails

•
• position    integer
        • model acts_as_list

class CreateUsers < ActiveRecord::Migration   class User < ActiveRecord::Base
 def self.up                                   acts_as_list
   create_table :users do |t|                  validates_presence_of :name
    t.column :name, :string                   end
    t.column :position, :integer
   end
 end

 def self.down
  drop_table :users
 end
end
•   decrement_position
•   first?
•   higher_item
•   in_list?
•   increment_position
•   insert_at
•   last?
•   lower_item
•   move_higher
•   move_lower
•   move_to_bottom
•   move_to_top
•   remove_from_list
position

        • acts_as_list :scope=>XXX

class CreateMobileSuits <              class MobileSuit < ActiveRecord::Base
ActiveRecord::Migration                 belongs_to :group
 def self.up                            acts_as_list :scope => :group
   create_table :mobile_suits do |t|   end
    t.column :group_id,:integer
    t.column :position,:integer
    t.column :name, :string
   end
 end

 def self.down
  drop_table :mobile_suits
 end
end
• XXcrement_position
 •

• remove_from_list
 •                     position nil
acts as tree




• Rails

•
• parent_id     integer
        • model acts_as_tree

class CreateDepartments < ActiveRecord::Migration   class Department < ActiveRecord::Base
 def self.up                                         acts_as_tree :order => :id
   create_table :departments do |t|                 end
    t.column :name,     :string
    t.column :parent_id, :integer
   end
 end

 def self.down
  drop_table :departments
 end
end
•   ancestors
•   root
•   self_and_siblings siblings+
•   siblings

      belongs_to   parent    has_many children
                            has_many belongs_to


department=Department.find(5)
department.children.create(:name=>”         ”)
department.root.name #
acts as tree

•

•

•



•
acts as nested set


• Rails
•
•            acts as tree
acts as nested set
root
 |_ Child 1
   |_ Child 1.1
   |_ Child 1.2
 |_ Child 2
   |_ Child 2.1
   |_ Child 2.2




                  leaf   select
• parent_id,lft,rgt  integer
  • model acts_as_nested_set
class CreateOrganizations < ActiveRecord::Migration class Organization < ActiveRecord::Base
 def self.up                                         acts_as_nested_set
   create_table :organizations do |t|               end
    t.column :name, :string
    t.column :parent_id, :integer
    t.column :lft, :integer
    t.column :rgt, :integer
   end
 end

 def self.down
  drop_table :organizations
 end
end
• add_child(child)
• all_children
• before_destroy

• child?
• children_count

• direct_children
• full_set

• root?

• unknown?
acts as nested set

•

    • before_destroy   add_child

•

    •
better nested set
•   http://d.hatena.ne.jp/xibbar/20060922#1159014466
      •   move_to_child_of
      •   move_to_right_of
      •   move_to_left_of
      •   root
      •   roots
      •   level
      •   ancestors
      •   self_and_ancestors
      •   siblings
      •   self_and_siblings
      •   children_count
      •   children
      •   all_children
      •   left_col_name
      •   right_col_name
      •   parent_col_name
•

•

More Related Content

Similar to Better nested set acts as list tree nested set

Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Prxibbar
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Pedro Cunha
 
Let it crash - fault tolerance in Elixir/OTP
Let it crash - fault tolerance in Elixir/OTPLet it crash - fault tolerance in Elixir/OTP
Let it crash - fault tolerance in Elixir/OTPMaciej Kaszubowski
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Brian Hogan
 
Parse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful SoupParse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful SoupJim Chang
 
PHP object calisthenics
PHP object calisthenicsPHP object calisthenics
PHP object calisthenicsGiorgio Cefaro
 
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...Coupa Software
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkBen Scofield
 
Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Roy Zimmer
 
Database schema management in Ruby apps
Database schema management in Ruby appsDatabase schema management in Ruby apps
Database schema management in Ruby appsVsevolod Romashov
 
Intro to Rails ActiveRecord
Intro to Rails ActiveRecordIntro to Rails ActiveRecord
Intro to Rails ActiveRecordMark Menard
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.VimLin Yo-An
 
Elixir Elevated: The Ups and Downs of OTP at ElixirConf2014
Elixir Elevated: The Ups and Downs of OTP at ElixirConf2014Elixir Elevated: The Ups and Downs of OTP at ElixirConf2014
Elixir Elevated: The Ups and Downs of OTP at ElixirConf2014Greg Vaughn
 
Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perltinypigdotcom
 

Similar to Better nested set acts as list tree nested set (20)

Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Pr
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11
 
Let it crash - fault tolerance in Elixir/OTP
Let it crash - fault tolerance in Elixir/OTPLet it crash - fault tolerance in Elixir/OTP
Let it crash - fault tolerance in Elixir/OTP
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7
 
Parse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful SoupParse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful Soup
 
PHP object calisthenics
PHP object calisthenicsPHP object calisthenics
PHP object calisthenics
 
Beware sharp tools
Beware sharp toolsBeware sharp tools
Beware sharp tools
 
DataMapper
DataMapperDataMapper
DataMapper
 
Rails OO views
Rails OO viewsRails OO views
Rails OO views
 
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...
 
Ruby Classes
Ruby ClassesRuby Classes
Ruby Classes
 
2013 28-03-dak-why-fp
2013 28-03-dak-why-fp2013 28-03-dak-why-fp
2013 28-03-dak-why-fp
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web Framework
 
Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)
 
Php Basic
Php BasicPhp Basic
Php Basic
 
Database schema management in Ruby apps
Database schema management in Ruby appsDatabase schema management in Ruby apps
Database schema management in Ruby apps
 
Intro to Rails ActiveRecord
Intro to Rails ActiveRecordIntro to Rails ActiveRecord
Intro to Rails ActiveRecord
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Elixir Elevated: The Ups and Downs of OTP at ElixirConf2014
Elixir Elevated: The Ups and Downs of OTP at ElixirConf2014Elixir Elevated: The Ups and Downs of OTP at ElixirConf2014
Elixir Elevated: The Ups and Downs of OTP at ElixirConf2014
 
Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perl
 

More from xibbar

エンジニア志望だったのにRuby社長をしてよかったと思う5つのこと
エンジニア志望だったのにRuby社長をしてよかったと思う5つのことエンジニア志望だったのにRuby社長をしてよかったと思う5つのこと
エンジニア志望だったのにRuby社長をしてよかったと思う5つのことxibbar
 
札幌Ruby会議03のlt
札幌Ruby会議03のlt札幌Ruby会議03のlt
札幌Ruby会議03のltxibbar
 
Fukushima.rb#00
Fukushima.rb#00Fukushima.rb#00
Fukushima.rb#00xibbar
 
仙台Ruby会議02 Pdf
仙台Ruby会議02 Pdf仙台Ruby会議02 Pdf
仙台Ruby会議02 Pdfxibbar
 
地域振興論2009
地域振興論2009地域振興論2009
地域振興論2009xibbar
 
栃木Ruby会議02 Lt途中まで
栃木Ruby会議02 Lt途中まで栃木Ruby会議02 Lt途中まで
栃木Ruby会議02 Lt途中までxibbar
 
tDiaryなどのレガシーウェブアプリをRuby1.9で動かす方法
tDiaryなどのレガシーウェブアプリをRuby1.9で動かす方法tDiaryなどのレガシーウェブアプリをRuby1.9で動かす方法
tDiaryなどのレガシーウェブアプリをRuby1.9で動かす方法xibbar
 
1000speakers仙台
1000speakers仙台1000speakers仙台
1000speakers仙台xibbar
 
More Better Nested Set
More Better Nested SetMore Better Nested Set
More Better Nested Setxibbar
 
Ruby&Active Support for expert 3
Ruby&Active Support for expert 3Ruby&Active Support for expert 3
Ruby&Active Support for expert 3xibbar
 
RubyとActive Support for expert 2
RubyとActive Support for expert 2RubyとActive Support for expert 2
RubyとActive Support for expert 2xibbar
 
Ruby and ActiveSupport for expart
Ruby and ActiveSupport for expartRuby and ActiveSupport for expart
Ruby and ActiveSupport for expartxibbar
 
Tohoku Open Source Conference 2008
Tohoku Open Source Conference 2008Tohoku Open Source Conference 2008
Tohoku Open Source Conference 2008xibbar
 

More from xibbar (13)

エンジニア志望だったのにRuby社長をしてよかったと思う5つのこと
エンジニア志望だったのにRuby社長をしてよかったと思う5つのことエンジニア志望だったのにRuby社長をしてよかったと思う5つのこと
エンジニア志望だったのにRuby社長をしてよかったと思う5つのこと
 
札幌Ruby会議03のlt
札幌Ruby会議03のlt札幌Ruby会議03のlt
札幌Ruby会議03のlt
 
Fukushima.rb#00
Fukushima.rb#00Fukushima.rb#00
Fukushima.rb#00
 
仙台Ruby会議02 Pdf
仙台Ruby会議02 Pdf仙台Ruby会議02 Pdf
仙台Ruby会議02 Pdf
 
地域振興論2009
地域振興論2009地域振興論2009
地域振興論2009
 
栃木Ruby会議02 Lt途中まで
栃木Ruby会議02 Lt途中まで栃木Ruby会議02 Lt途中まで
栃木Ruby会議02 Lt途中まで
 
tDiaryなどのレガシーウェブアプリをRuby1.9で動かす方法
tDiaryなどのレガシーウェブアプリをRuby1.9で動かす方法tDiaryなどのレガシーウェブアプリをRuby1.9で動かす方法
tDiaryなどのレガシーウェブアプリをRuby1.9で動かす方法
 
1000speakers仙台
1000speakers仙台1000speakers仙台
1000speakers仙台
 
More Better Nested Set
More Better Nested SetMore Better Nested Set
More Better Nested Set
 
Ruby&Active Support for expert 3
Ruby&Active Support for expert 3Ruby&Active Support for expert 3
Ruby&Active Support for expert 3
 
RubyとActive Support for expert 2
RubyとActive Support for expert 2RubyとActive Support for expert 2
RubyとActive Support for expert 2
 
Ruby and ActiveSupport for expart
Ruby and ActiveSupport for expartRuby and ActiveSupport for expart
Ruby and ActiveSupport for expart
 
Tohoku Open Source Conference 2008
Tohoku Open Source Conference 2008Tohoku Open Source Conference 2008
Tohoku Open Source Conference 2008
 

Better nested set acts as list tree nested set

  • 1.
  • 2. • acts as list • acts as tree • acts as nested set • better nested set
  • 3. acts as list • Rails •
  • 4. • position integer • model acts_as_list class CreateUsers < ActiveRecord::Migration class User < ActiveRecord::Base def self.up acts_as_list create_table :users do |t| validates_presence_of :name t.column :name, :string end t.column :position, :integer end end def self.down drop_table :users end end
  • 5. decrement_position • first? • higher_item • in_list? • increment_position • insert_at • last? • lower_item • move_higher • move_lower • move_to_bottom • move_to_top • remove_from_list
  • 6. position • acts_as_list :scope=>XXX class CreateMobileSuits < class MobileSuit < ActiveRecord::Base ActiveRecord::Migration belongs_to :group def self.up acts_as_list :scope => :group create_table :mobile_suits do |t| end t.column :group_id,:integer t.column :position,:integer t.column :name, :string end end def self.down drop_table :mobile_suits end end
  • 7. • XXcrement_position • • remove_from_list • position nil
  • 8. acts as tree • Rails •
  • 9. • parent_id integer • model acts_as_tree class CreateDepartments < ActiveRecord::Migration class Department < ActiveRecord::Base def self.up acts_as_tree :order => :id create_table :departments do |t| end t.column :name, :string t.column :parent_id, :integer end end def self.down drop_table :departments end end
  • 10. ancestors • root • self_and_siblings siblings+ • siblings belongs_to parent has_many children has_many belongs_to department=Department.find(5) department.children.create(:name=>” ”) department.root.name #
  • 12. acts as nested set • Rails • • acts as tree
  • 13. acts as nested set root |_ Child 1 |_ Child 1.1 |_ Child 1.2 |_ Child 2 |_ Child 2.1 |_ Child 2.2 leaf select
  • 14. • parent_id,lft,rgt integer • model acts_as_nested_set class CreateOrganizations < ActiveRecord::Migration class Organization < ActiveRecord::Base def self.up acts_as_nested_set create_table :organizations do |t| end t.column :name, :string t.column :parent_id, :integer t.column :lft, :integer t.column :rgt, :integer end end def self.down drop_table :organizations end end
  • 15. • add_child(child) • all_children • before_destroy • child? • children_count • direct_children • full_set • root? • unknown?
  • 16. acts as nested set • • before_destroy add_child • •
  • 17. better nested set • http://d.hatena.ne.jp/xibbar/20060922#1159014466 • move_to_child_of • move_to_right_of • move_to_left_of • root • roots • level • ancestors • self_and_ancestors • siblings • self_and_siblings • children_count • children • all_children • left_col_name • right_col_name • parent_col_name