Rails:自我加入

Pav*_*bin 1 ruby ruby-on-rails ruby-on-rails-4

我正在尝试设计一个与自身有关系的模型

模型:

class Department < ActiveRecord::Base

  belongs_to :organization
  has_many   :positions
  has_many   :sub_departments, class: 'Department', foreign_key: 'parent_id'

end
Run Code Online (Sandbox Code Playgroud)

移民:

class CreateDepartments < ActiveRecord::Migration
  def change
    create_table :departments do |t|
      t.string     :name
      t.references :parent, index: true
      t.references :organization, index: true

      t.timestamps
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

当我打电话时,Department.first.sub_departments我得到一个错误:NoMethodError: undefined method 'relation_delegate_class' for "Department":String.我究竟做错了什么?

谢谢!

saw*_*awa 6

我认为你应该用class_name:而不是class:.