bullet
bullet copied to clipboard
What do you expect has_one to has_one association?
Thanks very much for this great gem. I use it on every rails app.
I'd like to know what expected behavior is in a case of has_one to has_one association because looking into spec/integration
I didn't find any tests of has_one to has_one association.
In the following use case, bullet raises Bullet::Notification::UnoptimizedQueryError and indicates to add finder. But I don't expect any N + 1 query is created ( of course, no N+1 query in a log ).
Model
# user.rb
has_one :profile
# profile.rb
belongs_to :user
has_one :image
# image.rb
belongs_to :profile
Controller and View
# images_controller.rb
class ImagesController
before_action :authenticate_user!
before_action :set_profile
before_action :set_image
def show
end
private
def set_profile
@profile = current_user.profile # This raises Bullet::Notification::UnoptimizedQueryError
@profile = Profile.includes(:image).find_by(user_id: current_user.id) # This doesn't raise
end
def set_image
@image = @profile.image
end
end
# show.html.erb
<ul>
<li>@image.image_url</li>
<li>@image.file_name</li>
<li>@image.filesize</li>
</ul>
The error message is like this:
Bullet::Notification::UnoptimizedQueryError:
user: USERNAME
GET /user/image
USE eager loading detected
Profile => [:image]
Is that expected behavior? Thank you for reading, I hope someone answers.
@ebihara99999 it looks like a bug in bullet, will try to fix it later.
Hi @ebihara99999 👋
I got the same issue after trying to move to Rails 5.2.
~~How did you do to skip it in the checks ? I tried to add Bullet.add_whitelist :type =>
but I'm not sure about which type to precise~~
Edit: unused_eager_loading
works just fine 👍
@flyerhzm I'd like to give it a try, do you have any hint for me on this issue ? Is it a tricky fix ? Just writing new specs will be enough ? Do you have any recommandations on how to do it ?
Thanks !
still have same issue. I have has_one association but bullet recommend add includes... ( v6.1.0)
Bullet::Notification::UnoptimizedQueryError:
user: USERNAME
POST /api/test
USE eager loading detected
User => [:profile]
Add to your query: .includes([:profile])
Call stack
I still encountered the same issue on 2 cases with 1-to-1 relationships:
on user
I have has_one :profile, dependent: :destroy
and on profile
I have belongs_to :user
I do get the error:
USE eager loading detected
Profile => [:user]
Add to your finder: :includes => [:user]
when trying to do an update that cannot be an n+1 query
:
current_user.profile.update!(level: 'whatever')