flyerhzm / bullet

help to kill N+1 queries and unused eager loading
MIT License
7.06k stars 431 forks source link

Factory bot created resources - AVOID eager loading detected #435

Open tomca32 opened 5 years ago

tomca32 commented 5 years ago

This started happening after upgrade to 5.8.1 from 5.7.5 We use Factory bot to create resources for testing and when creating a record with association Bullet complains about unnecessary eager loading. Here's an example:

  @grade = create :grade, grade_descriptor: @grade_descriptor1 # factory bot create
  @grade.update_grade_descriptor @grade_descriptor2
  expect(@grade.grade_descriptor).to eq @grade_descriptor2
AVOID eager loading detected
  Grade => [:grade_descriptor]
  Remove from your finder: :includes => [:grade_descriptor]

I guess that it doesn't like the fact that the first grade_descriptor was loaded and didn't even get used, which makes sense; however, it would be great if it could somehow ignore resources created via factories.

sandipsubedi commented 5 years ago

We are having the same issue.

ArneZsng commented 5 years ago

Same here. No solution in sight yet?

tsolar commented 5 years ago

same here

jann commented 5 years ago

I encounter the same problem. Any hints highly appreciated.

ArneZsng commented 5 years ago

My observation is that it works in the combination of bullet (5.7.5) and factory_bot (4.11.1), but neither with bullet (5.9.0) nor bullet (5.7.6).

tsolar commented 5 years ago

I am still having problems with 5.7.5.

I have a factory like this

FactoryBot.define do
  factory :ticket do
    ...
    trip_segment do
      trip = create(:trip)
      trip.trip_segments.first
    end
    ...
  end
end

And I get this

USE eager loading detected
         Ticket => [:trip]
         Add to your finder: :includes => [:trip]

What is wrong?

tsolar commented 5 years ago

I checked if it works with bullet (5.9.0), and it does. The spec using that factory passes, but having the same AVOID eager loading problem with others.

spheric commented 5 years ago

It looks like for my problem the PR that introduced it was https://github.com/flyerhzm/bullet/pull/418

can replicate it using

class Foo < ActiveRecord::Base
  belongs_to :bar
end

class Bar < ActiveRecord::Base
end

Foo.create; Bar.create

Bullet.profile do
  foo = Foo.first
  foo.bar = Bar.first
end
AVOID eager loading detected
  Foo => [:bar]
  Remove from your finder: :includes => [:bar]

I have quite a limited understanding of how bullet works but Is this the intended behaviour @ccutrer ?

spheric commented 5 years ago

@flyerhzm Any update on this issue?

dandunckelman commented 2 years ago

Anything new we can do on this?

dandunckelman commented 2 years ago

Anything new we can do on this?

Can we use the safelist for factories?