モデル シナリオ:
A node can belong to a parent node and can have child nodes.
models/node.rb
class Node < ActiveRecord::Base
has_many :children, class_name: "Node", foreign_key: "parent_id"
belongs_to :parent, class_name: "Node"
end
デシベル/移行/20131031144907_create_nodes.rb
class CreateNodes < ActiveRecord::Migration
def change
create_table :nodes do |t|
t.timestamps
end
end
end
そして、リレーションを追加するために移行を実行したいと思います:
class AddNodesToNodes < ActiveRecord::Migration
def change
add_column :nodes, :parent_id, :integer
# how do i add childen?
end
end
移行に has_many 関係を追加するにはどうすればよいですか?