在 Rails 7 中测试过滤急切加载的数据时出错

问题描述 投票:0回答:0

目前在我的索引操作中,我将数据加载为:

posts.includes([:user, :status])

注意:帖子属于一个用户(可选)。有些帖子没有用户。

然后我根据不同的参数过滤这些帖子。 一切都很完美,直到我试图过滤没有用户的帖子。

scope :no_user, -> { where(user_id: nil) }

当我尝试在浏览器中测试行为时没有出现错误,但是当使用此特定过滤器发出 GET 请求时,我的控制器测试(Minitest)失败了。

Bullet::Notification::UnoptimizedQueryError
AVOID eager loading detected
  Post => [:user]
  Remove from your query: .includes([:user])

有趣的是,当我从数组中删除 :user 时,我在其他过滤器测试中得到了完全相反的错误。

USE eager loading detected
  Post => [:user]
  Add to your query: .includes([:user])

注意:如果我尝试使用

left_outer_joins(:user)

,我会得到同样的错误

有没有一种方法可以在没有条件语句的情况下仅针对此特定过滤器消除测试中的此错误?

ruby-on-rails include minitest eager-loading ruby-on-rails-7
© www.soinside.com 2019 - 2024. All rights reserved.