USE eager loading detected[Bulllet Gem]
Asked Answered
L

2

5

I am using Rails 5.2.2 . I installed the Bullet Gem and getting this on bullet.log :

2019-01-18 13:22:02[WARN] user: jordan
GET /customers/37
USE eager loading detected
  Account => [:user]
  Add to your finder: :includes => [:user]
Call stack
  /home/jordan/Desktop/Rails/acc/app/views/customers/show.html.erb:64:in `block in _app_views_customers_show_html_erb___50963452045031815_70292526903360'
  /home/jordan/Desktop/Rails/acc/app/views/customers/show.html.erb:62:in `_app_views_customers_show_html_erb___50963452045031815_70292526903360'

According to the guide i added includes(:user) to my account.rb file. I tried to add it to index action and show action both but this alert comes in again and again.

Account.rb:

class Account < ApplicationRecord
  belongs_to :user
  belongs_to :customer
end

accounts_controller.rb:

  def index
    @accounts = Account.includes(:user).where(customer_id:params[:id])
  end

  private

  def set_account
    @account = Account.includes(:user).find(params[:id])
  end

show.html.erb (customers 62..64 lines)

<% @customer.accounts.each do |ac| %>
   <div class="sl-item">
       <div class="sl-left"> <img src="<%= ac.user.profile_picture %>" alt="user" class="img-circle" /> </div>

How can i fix that?

Least answered 18/1, 2019 at 11:49 Comment(2)
Please, add code from views/customers/show.html.erb. The error cause in line 64 (look at the error stacktrace), so please add code block, containing this line.Quaternary
@Quaternary thanks i added to the main post.Least
Q
5

You can add one more instance variable, but it increases size of controller.action. Second option:

# you didn't a add real code where you define @customer, so it just example
@customer = Customer.includes(accounts: :user).find(params[:customer_id])
Quaternary answered 18/1, 2019 at 13:10 Comment(1)
@customer = Customer.includes(accounts: :user).find(params[:id]) This solved my problem. Thanks.Least
K
1

I suggest you to use following in html,

<% @accounts.each do |ac| %>

where, inside action you have to create instance variable as,

@accounts = @customer.accounts.includes(:user)
Kedah answered 18/1, 2019 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.