Rails 认为 "faves" 指的是名为 "Fafe" 的模型

Rails thinks "faves" is referring to a model named "Fafe"

我有一个名为 Fave 的模型。它属于另一个名为 User.

的模型

我正在尝试通过调用 @user.faves 查找用户的最爱。我的服务器返回以下错误:

NameError: uninitialized constant User::Fafe

为什么会认为"faves"的单数是"fafe"?我可以使用其他指向 "fave" 的复数形式吗?

我们可以在 config/initializers/inflections.rb 试试这个吗?这可行

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'fave', 'faves' #append this to the existing ones
end

您可以在设置关联时传递 class 名称

has_many :faves, class_name: "Fave"