【发布时间】:2013-10-31 15:47:54
【问题描述】:
模型场景:
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关系?
【问题讨论】:
-
据我所知,您已经完成了您需要做的所有事情。你有什么错误吗?
标签: ruby-on-rails ruby ruby-on-rails-4