Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails 5.0中使用postgresql的自连接_Ruby On Rails - Fatal编程技术网

Ruby on rails rails 5.0中使用postgresql的自连接

Ruby on rails rails 5.0中使用postgresql的自连接,ruby-on-rails,Ruby On Rails,我正在尝试在rails中创建自连接 class ItemGroup < ApplicationRecord has_many :children, class_name: "ItemGroup", foreign_key: "parent_id" belongs_to :parent, class_name: "ItemGroup" end class ItemGroup

我正在尝试在rails中创建自连接

class ItemGroup < ApplicationRecord
  has_many :children, class_name: "ItemGroup", foreign_key: "parent_id"

  belongs_to :parent, class_name: "ItemGroup"
end
class ItemGroup
这是我的代码,但当我尝试添加新记录时,它不会保存它。那么,这段代码中的问题是什么?任何人都可以帮助我,或者告诉我有关自连接的想法

class CreateItemGroups < ActiveRecord::Migration[5.0]
  def change
    create_table :item_groups do |t|
      t.string :name
      t.integer :parent_id
      t.text :description
      t.references :parent, index: true

      t.timestamps
    end
  end
end
class CreateItemGroups

这是我的迁移文件。

Rails 5使
属于默认情况下所需的关联。要禁用它,您需要将
可选:true
添加到
所属

belongs_to :parent, class_name: "ItemGroup", optional: true

非常感谢。它起作用了。。。所以你能告诉我如何访问数据吗?我也尝试过使用它,但它没有提供我的名字这里f是一个循环参数。不,当然,但可能
name
是空的<代码>父项
记录存在于
f
中。尝试更改为