尝试包含 class/object 关联时出现未初始化的常量错误
uninitialized constant error when trying to include class/object association
我一直在尝试将 User 对象传递给变量并使用 includes
包含其 UserSettings 相关对象方法:
logged_user = User.includes(:user_settings).find_by(login: login)
但我不断收到以下错误:
"error": "uninitialized constant User::UserSettings"
这里是 类' 的定义:
class UserSetting < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_one :user_settings, :class_name => 'UserSettings'
has_many :sessions
scope :active, -> { where(active: true) }
end
我做错了什么?
在
has_one :user_settings, :class_name => 'UserSettings'
尝试将关联复数 user_settings
更改为 user_setting
并且您可以删除 class_name
或将复数 UserSettings
删除为单数 UserSetting
,就像这个:
has_one :user_setting, :class_name => 'UserSetting'
然后将 includes user_settings
更改为 user_setting
logged_user = User.includes(:user_setting).find_by(login: login)
我希望这对你有用。
我一直在尝试将 User 对象传递给变量并使用 includes
包含其 UserSettings 相关对象方法:
logged_user = User.includes(:user_settings).find_by(login: login)
但我不断收到以下错误:
"error": "uninitialized constant User::UserSettings"
这里是 类' 的定义:
class UserSetting < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_one :user_settings, :class_name => 'UserSettings'
has_many :sessions
scope :active, -> { where(active: true) }
end
我做错了什么?
在
has_one :user_settings, :class_name => 'UserSettings'
尝试将关联复数 user_settings
更改为 user_setting
并且您可以删除 class_name
或将复数 UserSettings
删除为单数 UserSetting
,就像这个:
has_one :user_setting, :class_name => 'UserSetting'
然后将 includes user_settings
更改为 user_setting
logged_user = User.includes(:user_setting).find_by(login: login)
我希望这对你有用。