bullet: 8.0.1 regression: eager loading errors on destroy

When deleting a model with has_many ..., dependent: destroy associations, the switch from v8.0.0 to v8.0.1 introduces UnoptimizedQueryError exceptions to .destroy tests:

     Failure/Error: Bullet.perform_out_of_channel_notifications if Bullet.notification?
     
     Bullet::Notification::UnoptimizedQueryError:
       user: user
        
       USE eager loading detected
         DependentModel => [:from_user]
         Add to your query: .includes([:from_user])
       Call stack
         /home/user/Documents/myapp/app/models/dependent_model.rb:23:in `extra'
         /home/user/Documents/myapp/spec/models/my_model_spec.rb:519:in `block (4 levels) in <top (required)>'
         /home/user/Documents/myapp/spec/models/my_model_spec.rb:519:in `block (3 levels) in <top (required)>'
     # ./spec/rails_helper.rb:75:in `block (2 levels) in <top (required)>'

Test environment:

  config.after_initialize do
    Bullet.enable                      = true
    Bullet.bullet_logger               = true
    Bullet.raise                       = true # raise an error if n+1 query occurs
    Bullet.unused_eager_loading_enable = false
    Bullet.counter_cache_enable = false
  end

In rails_helper.rb:

RSpec.configure do |config|
  ...
  if Bullet.enable?
    config.before do
      Bullet.start_request
    end

    config.after do
      Bullet.perform_out_of_channel_notifications if Bullet.notification?
      Bullet.end_request
    end
  end
end

The app is on Rails 6.1 and Ruby 3.1.

This looks to be related to versioning (using the paper_trail gem):

class DependentModel < ApplicationRecord
  belongs_to :my_model

  has_paper_trail meta: { my_model_id: :my_model_id, extra: :extra },
    versions: { class_name: 'ModelVersion' }

  def extra
    JSON.dump({ from_user_name: from_user.name, from_user_email: from_user.email,
to_user_name: to_user.name, to_user_email: to_user.email })
  end

Just can’t pin down why this bug was introduced in v8.0.1. The only workaround is to add has_many :dependent_model, -> { include(:from_user, :to_user) }, dependent: :destroy, but then we are eager loading in every case.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

@brianclinkenbeard A new version has been released if you’d be willing to jump on 8.0.2, it should solve the issues you saw.

Giving this a run in our CI to see if it resolves things for us…

Update: this resolved our broken spec suite from 8.0.1

Amusingly, when I updated to 8.0.3 I think I got a ton of spec breakages again, but haven’t dug in yet 😆

FWIW, I see this issue in my code base as well. I’d be happy to try any repairs and report back.

@brianclinkenbeard A new version has been released if you’d be willing to jump on 8.0.2, it should solve the issues you saw.

Giving this a run in our CI to see if it resolves things for us…

Update: this resolved our broken spec suite from 8.0.1

The repo is rather large and private, but the test suite fails reliably for me in local and CI. I can bundle a branch with a proposed fix to debug or test a repair. If it’s helpful, I can try to describe my issue, but my issue seems to mirror the Post/Comment example shown above.