bullet icon indicating copy to clipboard operation
bullet copied to clipboard

False N+1 Query for delegate field

Open jonopray opened this issue 9 years ago • 4 comments

I've run into an issue where bullet is finding a N+1 query on an object that has a delegate.

The query is like: Foo.includes(bar: [:baz]).find(id)

Inside Foo: has_many :bars

Inside Bar:

belongs_to :baz, class_name 'BazFullName'
delegate :some_field, to: :baz

Bullet spits out:

15:10:16 web.1     | N+1 Query detected
15:10:16 web.1     |   Bar => [:baz]
15:10:16 web.1     |   Add to your finder: :includes => [:baz]
15:10:16 web.1     | N+1 Query method call stack
15:10:16 web.1     |   /app/models/bar.rb:10:in `some_field'

jonopray avatar Sep 30 '15 22:09 jonopray

@prismatica is it a typo? bar vs bars ?

flyerhzm avatar Oct 11 '15 02:10 flyerhzm

I also have the problem that despite using includes in my query, I get the N+1 Query detected message, once I call a method on the primary model, that is delegated to the included model. I started to think that eager-loading doesn't work, when delegating a method, but tested this in the console and saw that the right query is produced. Will try to create a sample app, but I'm not sure when I'll find time for this.

AlexVPopov avatar Aug 25 '16 07:08 AlexVPopov

Given the new delegated_type in Rails 6.1 is there any movement or any help necessary to work on this?

cguess avatar Jun 03 '21 14:06 cguess