我有一种感觉,这是一个小的"s"缺失,但48小时后,我找不到我的错误:
我有两个模型:用户和Cookie。一个用户可以拥有许多Cookie。饼干属于饼干。
当我尝试做一个简单的current_user.cookies时,我会出现以下错误:
uninitialized constant User::Cooky
我反复检查了一下我是否在某个地方有复数错误,但我没有发现任何错误。
这是我的cookie表迁移
class CreateCookies < ActiveRecord::Migration[5.1]
def change
create_table :cookies do |t|
t.string :value
t.string :value2
t.references :user, foreign_key: true
t.references :network, foreign_key: true
t.timestamps
end
end
end
cookie.rb
class Cookie < ApplicationRecord
belongs_to :network
belongs_to :user
end
user.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :room_users
has_many :rooms, through: :room_users
has_many :cookies, dependent: :destroy
end
提前感谢您的帮助。
看起来像是一个复数错误,可能您在config/initializers/inflections.rb
或任何使用ActiveSupport::Inflector.inflections
的地方设置了一些自定义屈折
Dorian谢谢你让我走上正轨。这是一个屈折的问题:我添加了屈折。rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'cookie', 'cookies'
end
现在它工作了!!