Rails:自我加入

时间:2014-07-16 10:08:24

标签: ruby-on-rails ruby 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

迁移:

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

当我致电Department.first.sub_departments时,我收到错误:NoMethodError: undefined method 'relation_delegate_class' for "Department":String。我究竟做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:6)

我认为您应该使用class_name:代替class: