bullet icon indicating copy to clipboard operation
bullet copied to clipboard

Bullet detecting N+1 query in Memory objects

Open ezekielriva opened this issue 2 months ago • 1 comments

Problem: bullet is detecting N+1 query in one object already in memory

Example Code:

describe "Memory Error" do
  let(:object_a) { create(:object_a) }
  let(:object_b) { create(:object_b, object_a:) }
  let!(:object_c) { create(:object_c, object_b:) }

  it "fails" do
     object_c.object_b.object_a # this trigger missing N+1 query
  end
end

Models:

class ModelA
  has_one :model_b, class_name: "ModelB", dependent: :destroy, inverse_of: :model_a
end

class ModelB
  has_many :model_cs, class_name: "ModelC", dependent: :destroy, inverse_of: :model_b
  belongs_to :model_a, polymorphic: true, inverse_of: :model_b
end

class ModelC
  belongs_to :model_b, class_name: "ModelB", inverse_of: :model_cs
end

Bullet Debug:

[Bullet][Detector::Association#add_call_object_associations] object: ModelC:814, associations: model_b
[Bullet][Detector::NPlusOneQuery#call_association] object: ModelC:814, associations: model_b
[Bullet][Detector::NPlusOneQuery#add_possible_objects] objects: ModelB:859
[Bullet][Detector::Association#add_call_object_associations] object: ModelB:859, associations: model_as
[Bullet][Detector::NPlusOneQuery#call_association] object: ModelB:859, associations: model_as
[Bullet][detect n + 1 query] object: ModelB:859, associations: model_as
[Bullet][Detector::NPlusOneQuery#add_possible_objects] objects: ModelA:1518

Configuration:

# Gemfile.lock
activerecord (7.2.2.2)
      activemodel (= 7.2.2.2)
      activesupport (= 7.2.2.2)
      timeout (>= 0.4.0)

# .ruby-version
3.4.5

ezekielriva avatar Oct 27 '25 16:10 ezekielriva

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Nov 27 '25 02:11 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Dec 11 '25 02:12 github-actions[bot]