ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
²¹³¦³Ù²õ³å²¹²õ¤òʹ¤Ã¤Æ¤ß¤è¤¦
? 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
?   ?rst?
?   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.?nd(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

²¹³¦³Ù²õ³å²¹²õ¤òʹ¤Ã¤Æ¤ß¤è¤¦

  • 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 ? ?rst? ? 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.?nd(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
  • 18. ? ?