• 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 < ActiveRecor...
•   decrement_position
•   first?
•   higher_item
•   in_list?
•   increment_position
•   insert_at
•   last?
•   lower_ite...
position

        • acts_as_list :scope=>XXX

class CreateMobileSuits <              class MobileSuit < ActiveRecord::Base...
• XXcrement_position
 •

• remove_from_list
 •                     position nil
acts as tree




• Rails

•
• parent_id     integer
        • model acts_as_tree

class CreateDepartments < ActiveRecord::Migration   class Department...
•   ancestors
•   root
•   self_and_siblings siblings+
•   siblings

      belongs_to   parent    has_many children
      ...
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




      ...
• parent_id,lft,rgt  integer
  • model acts_as_nested_set
class CreateOrganizations < ActiveRecord::Migration class Organi...
• add_child(child)
• all_children
• before_destroy

• child?
• children_count

• direct_children
• full_set

• root?

• un...
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_...
•

•
acts_asを使ってみよう
Upcoming SlideShare
Loading in …5
×

acts_asを使ってみよう

1,926 views

Published on

Rails勉強会@東北の資料です

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,926
On SlideShare
0
From Embeds
0
Number of Embeds
59
Actions
Shares
0
Downloads
8
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

acts_asを使ってみよう

  1. 1. • acts as list • acts as tree • acts as nested set • better nested set
  2. 2. acts as list • Rails •
  3. 3. • 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
  4. 4. • 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
  5. 5. 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
  6. 6. • XXcrement_position • • remove_from_list • position nil
  7. 7. acts as tree • Rails •
  8. 8. • 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
  9. 9. • 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 #
  10. 10. acts as tree • • • •
  11. 11. acts as nested set • Rails • • acts as tree
  12. 12. acts as nested set root |_ Child 1 |_ Child 1.1 |_ Child 1.2 |_ Child 2 |_ Child 2.1 |_ Child 2.2 leaf select
  13. 13. • 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
  14. 14. • add_child(child) • all_children • before_destroy • child? • children_count • direct_children • full_set • root? • unknown?
  15. 15. acts as nested set • • before_destroy add_child • •
  16. 16. 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
  17. 17. • •

×