未定义的方法 `relation_delegate_class' 为

undefined method `relation_delegate_class' for

此代码引发错误:

Employee.first.company.employees

错误文本:

NoMethodError (undefined method `relation_delegate_class' for Company::Employee:Module)

同时,有此代码的链接:

Employee.first.country.employees

而且效果很好。

我不明白为什么公司会出现错误。模型就在附近。其中,公司和国家代码几乎相同。链接是直接的,索引也是直接的,没有 class_name 之类的。

class Employee < ApplicationRecord
  # ...

  belongs_to :company

  belongs_to :country, optional: true

  validates :company, presence: true

  # ...
class Company < ApplicationRecord
  has_many :employees, dependent: :destroy

  # ...
class Country < ApplicationRecord
  has_many :employees, dependent: :nullify

  # ...

为什么这可能不起作用?

NoMethodError (undefined method relation_delegate_class..) 可能是由于多个 classes/modules 具有相同的名称,即使在不同的文件夹中(由于 autoload 的工作方式)