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

acts_asを使ってみよう

on

  • 1,996 views

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

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

Statistics

Views

Total Views
1,996
Views on SlideShare
1,996
Embed Views
0

Actions

Likes
2
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    acts_asを使ってみよう acts_asを使ってみよう Presentation Transcript

    • • 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
    • • •