ruby-on-rails - 设计 current_user 不是用户实例
问题描述
我正在使用带有设计 4.7.2 的 Rails 6.0.3.2
在我的测试中,我遇到了一个意外错误:
ActiveRecord::AssociationTypeMismatch: User(#47437210347680) expected, got #<User id: 1, email: ...> which is an instance of User(#47437198887480)
使用byebug
,确实 current_user 不是用户实例!
(byebug) current_user.class.__id__
47437198887480
(byebug) User.__id__
47437210347680
所以我的协会@contest.users << current_user
失败了......
有趣的是,如果我重置我的数据库,我在运行测试时第一次尝试就没有问题!
我在 gitlab 上创建了这个未解决的问题,但没有解决方法。
非常感谢您的帮助!
编辑
这是我在用户和竞赛之间的关联
# contest.rb
# frozen_string_literal: true
class Contest < ApplicationRecord
has_and_belongs_to_many :users
end
# user.rb
# frozen_string_literal: true
class User < ApplicationRecord
has_and_belongs_to_many :contests
end
我注意到在 app/ 目录中编辑文件后会出现问题。我可以编辑我的测试文件而不会导致此问题。所以我怀疑在某个地方重新加载了导致这种分歧的类......
解决方案
推荐阅读
- css - 试图获取 section.class1 而不是 section.class1, scss
- html - 当表格垂直扩展至其最大高度时 - 使元素在下一个表格单元格列中弹出
- sql-server - 无法使用 SQL Server 数据库访问 Cloud SQL 上的导入数据
- php - phpoffice/电子表格如何在图表中为每一列添加一个值
- node.js - 使用 Express 在 Node.js 中返回调用不起作用
- python - 使用列表中的索引值从大型数据帧创建较小的数据帧
- javascript - TypeScript 枚举到特定对象
- php - 无法打开 PHP,dyld:库未加载
- spring-boot - Spring-Kafka 似乎阻止 Spring Boot web 正常运行
- ibm-appid - 我无法在 Kubernetes 上安装 app-identity-and-access-adapter