模型场景:
A node can belong to a parent node and can have child nodes.
模型/node.rb
class Node < ActiveRecord::Base
has_many :children, class_name: "Node", foreign_key: "parent_id"
belongs_to :parent, class_name: "Node"
end
db/migrations/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 关系?