简体   繁体   English

如何为属于自身的rails模型编写迁移

[英]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: 特别:

  1. parent - The Node with the id corresponding to the value of the parent_id attribute for the current Node. parent - 具有与当前节点的parent_id属性值对应的id的节点。

  2. 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.

相关问题 Rails 5:将带有自定义名称的belongs_to 关联添加到模型和迁移 - Rails 5: Add belongs_to association with custom name to model, and migration 在Rails中,如何将一个模型两次连接到一个多态属于的另一个模型? - In Rails, how to connect a model twice to another model that it polymorphically belongs_to? 两个belongs_to关联迁移的模型相同 - Same Model for Two belongs_to Associations migration 如何在Rails中使用属于自己的模型进行查询 - How to Query Using a Model that Belongs To Itself in Rails 如何在rails 4中创建新的belongs_to关联模型 - how to create new a belongs_to associate model in rails 4 如何确定依赖于Emirates_to关联的Rails模型ID? - How to scope Rails model id which is dependent on belongs_to association? Rails 3-如何表示属于城市的用户模型? - Rails 3 - How to represent User model which belongs_to a City? 我如何在模型中对对象属轨中的.count进行计数? - How do I do a .count on the model an object belongs_to in rails? Rails 3 - 与自身具有一对一关系的 model - 我需要belongs_to - Rails 3 - A model with a one to one relationship to itself - do I need belongs_to 在Rails模型中等效于Emirates_to Through - Equivalent of belongs_to through in rails model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM