首页 > 解决方案 > 设计 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/ 目录中编辑文件后会出现问题。我可以编辑我的测试文件而不会导致此问题。所以我怀疑在某个地方重新加载了导致这种分歧的类......

标签: ruby-on-railsdevise

解决方案


推荐阅读