我尝试设计一个与自身有关的模型
型号:
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
。我究竟做错了什么?
谢谢!
答案 0 :(得分:6)
我认为您应该使用class_name:
代替class:
。