bullet
bullet copied to clipboard
False N+1 Query for delegate field
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'
@prismatica is it a typo? bar vs bars ?
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.
Given the new delegated_type in Rails 6.1 is there any movement or any help necessary to work on this?