This document discusses different ways that models can be structured and organized in Rails using acts_as_list, acts_as_tree, and acts_as_nested_set. Acts_as_list allows models to be organized and reordered as a simple list. Acts_as_tree allows models to be organized hierarchically with parent-child relationships like a tree. Acts_as_nested_set allows complex hierarchical relationships and includes additional methods for managing the tree structure.
1 of 18
Download to read offline
More Related Content
²¹³¦³Ù²õ³å²¹²õ¤òʹ¤Ã¤Æ¤ß¤è¤¦
2. ? acts as list
? acts as tree
? acts as nested set
? better nested set
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
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
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
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