rails:1555
From: Fujioka <fuj@r...>
Date: Thu, 24 Aug 2006 12:01:32 +0900
Subject: [rails:1555] 自分自身でhas_manyをしたい
藤岡です。
ちょっと行き詰まっているのでアイデアをいただければと思います。
ツリー状の組織を表現するテーブルを作っているのですが、
create_table :sections do |t|
t.column :name, :string
end
こんな感じです。これをツリー状に持っていくために
アイデアとしては
create_table :sections do |t|
t.column :section_id :integer
t.column :name, :string
end
というのを追加して、モデルで
class Section < ActiveRecord::Base
has_many :sections
end
とすると、コンソールで
>> section=Section.find(:first)
=> #<Section:0x40c35f1c @attributes={"name"=>"honbu", "section_id"=>nil,
"id"=>"1", "sort"=>nil}>
>> section.sections
=> [#<Section:0x40c25aa4 @attributes={"name"=>"eigyouka",
"section_id"=>"1", "id"=>"2", "sort"=>nil}>, #<Section:0x40c25a68
@attributes={"name"=>"kikakuka", "section_id"=>"1", "id"=>"3",
"sort"=>nil}>]
といった感じで一応自分自身でhas_manyはできるのがわかったのですが、
正しい方法なのかどうかがわかりません。
みなさんこういうテーブルはどうされていますか?
--
Fujioka (xibbar)
--
ML: rails@r...
使い方: http://QuickML.com/
-> 1555 2006-08-24 05:01 [fuj@r... ] 自分自身でhas_manyをしたい 1556 2006-08-24 05:18 ┗[kazuhiko@f... ] 1557 2006-08-24 05:41 ┣[fuj@r... ] 1558 2006-08-24 07:57 ┗[fuj@r... ]