[英]How to write a migration for a rails model that belongs_to itself
model scenario: 模型场景:
A node can belong to a parent node and can have child nodes.
models/node.rb 车型/ 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 分贝/迁移/ 20131031144907_create_nodes.rb
class CreateNodes < ActiveRecord::Migration
def change
create_table :nodes do |t|
t.timestamps
end
end
end
And then I want to do I migration to add the relations: 然后我想做迁移添加关系:
class AddNodesToNodes < ActiveRecord::Migration
def change
add_column :nodes, :parent_id, :integer
# how do i add childen?
end
end
How do i add the has_many relationship in the migratation? 如何在迁移中添加has_many关系?
You've done everything you needed to do.You can find the more informations in this page: 您已经完成了所需的一切。您可以在此页面中找到更多信息:
Source: http://guides.rubyonrails.org/association_basics.html 资料来源: http : //guides.rubyonrails.org/association_basics.html
node.parent
will find the parent_id
is the node id and return the parent. node.parent
将查找parent_id
是节点id并返回父节点。
node.children
will find the parent_id
is the node id and return the children. node.children
将发现parent_id
是节点id并返回子节点。
And when you add relations, you can do like this in Rails 4: 当你添加关系时,你可以在Rails 4中这样做:
## rails g migration AddNodesToNodes parent:belongs_to
class AddNodesToNodes < ActiveRecord::Migration
def change
add_reference :nodes, :parent, index: true
end
end
Per RailsGuides , this is an example of a Self-Join. Per RailsGuides ,这是一个自我加入的例子。
# model
class Node < ActiveRecord::Base
has_many :children, class_name: "Node", foreign_key: "parent_id"
belongs_to :parent, class_name: "Node"
end
# migration
class CreateNodes < ActiveRecord::Migration
def change
create_table :nodes do |t|
t.references :parent, index: true
t.timestamps null: false
end
end
end
There's no need for anything additional in the migration. 迁移中不需要任何其他内容。 The parent_id is used to define the relationship in both directions. parent_id用于定义两个方向的关系。 Specifically: 特别:
parent - The Node with the id corresponding to the value of the parent_id attribute for the current Node. parent - 具有与当前节点的parent_id属性值对应的id的节点。
children - All Nodes with a parent_id value corresponding to the value of the id attribute for the current Node. children - 具有parent_id值的所有节点,该值对应于当前节点的id属性的值。
You've already written the migration with your AddNodeToNodes and the parent ID. 您已经使用AddNodeToNodes和父ID编写了迁移。
That defines it at the database level. 这在数据库级别定义它。
At the 'rails' level (ActiveRecord) you define the has_many in the model definition, ie the Node.rb file that define the Node class. 在'rails'级别(ActiveRecord),您可以在模型定义中定义has_many,即定义Node类的Node.rb文件。
There is no 'migration' to add has_many. 添加has_many没有“迁移”。 Migrations are used for database fields (and indexes, etc.) such a as parent_id
but not for rails style relationship definitions such as has_many
. 迁移用于数据库字段(和索引等),例如parent_id
但不用于rails样式关系定义,例如has_many
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.