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

Like this? Share it with your network

Share

acts_asを使ってみよう

  • 2,103 views
Uploaded on

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

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

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,103
On Slideshare
2,103
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
6
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. • acts as list • acts as tree • acts as nested set • better nested set
  • 2. acts as list • Rails •
  • 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. • 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. 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. • XXcrement_position • • remove_from_list • position nil
  • 7. acts as tree • Rails •
  • 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. • 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. acts as tree • • • •
  • 11. acts as nested set • Rails • • acts as tree
  • 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. • 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. • add_child(child) • all_children • before_destroy • child? • children_count • direct_children • full_set • root? • unknown?
  • 15. acts as nested set • • before_destroy add_child • •
  • 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. • •