【发布时间】:2020-04-24 12:50:30
【问题描述】:
Rails 4.1.16
Ruby 2.2.7
我有这个 ActiveRecord 模型:
class Something::Type < ActiveRecord::Base
当像这样引用模型时:
Something::Type.find_by_whatever("test")
我收到以下错误:
NoMethodError: undefined method `find_by_whatever' for ActiveRecord::AttributeMethods::Serialization::Type:Class
我知道 Ruby 常量被分解了,所以 Type 作为自己的常量存在,自动加载器首先找到 ActiveRecord::AttributeMethods::Serialization::Type。
但是,以“绝对”方式(以冒号为前缀)引用命名空间应该可以解决问题,但结果是相同的。任何想法为什么?
::Something::Type.find_by_whatever("test")
NoMethodError: undefined method `find_by_whatever' for ActiveRecord::AttributeMethods::Serialization::Type:Class
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 autoload autoloader