【问题标题】:undefined method `relation_delegate_class' for未定义的方法“relation_delegate_class”
【发布时间】:2020-12-15 12:11:10
【问题描述】:

此代码抛出错误:

Employee.first.company.employees

错误文本:

NoMethodError(Company::Employee:Module 的未定义方法 `relation_delegate_class')

同时,有这段代码的链接:

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

  # ...

由于什么原因这可能不起作用?

【问题讨论】:

  • 请显示您的数据库结构/数据
  • @Thanh 请澄清您的问题。我无法展示整个结构。
  • 您还有其他名为 Company 的类/模块,甚至是嵌套的吗?
  • @DanielSindrestean 模板中只使用了“公司”(来自 ViewComponent;例如:Companies::ListComponent.new)。否则我会使用“CompanySomeWord”之类的东西。
  • @Colibri Just Employee, Company, Country 表就足够了。我只是想知道你如何建立他们的关系

标签: ruby-on-rails activerecord ruby-on-rails-6


【解决方案1】:

NoMethodError (undefined method relation_delegate_class..) 可能是由多个具有相同名称的类/模块引起的,即使它们位于不同的文件夹中(因为 autoload 的工作方式)

【讨论】:

    猜你喜欢
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 2013-06-11
    • 2010-12-30
    • 2019-12-25
    相关资源
    最近更新 更多