11. 関连テーブル
user.rb section.rb
class Section < ActiveRecord::Base
class CreateUsers < ActiveRecord::Migration
acts_as_nested_set
def self.up
acts_as_section_map
create_table :users do ?t?
has_many :users
t.column :name, :string, :null=>false
end
t.column :section_id, :integer
end
end
def self.down
drop_table :users
end
end
class User < ActiveRecord::Base
belongs_to :section
end
25. benchmark
% ./script/performance/benchmarker 100 'Section.?nd(:all).map(&:level)'
user system total real
#1 5.790000 0.470000 6.260000 ( 10.331682)
% ./script/performance/benchmarker 100 'Section.?nd(:all).map(&:depth)'
user system total real
#1 0.610000 0.010000 0.620000 ( 0.784886)
ちなみにindex張る前は
% ./script/performance/benchmarker 100 'Section.?nd(:all).map(&:level)'
user system total real
#1 6.180000 0.530000 6.710000 ( 20.693455)
20.693 0.784=26.394
26倍早くなった!!