检测到使用急切加载[Bullet Gem]

Zog*_*imu 5 ruby-on-rails ruby-on-rails-5

我正在使用 Rails 5.2.2 。我安装了 Bullet Gem 并将其打开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'
Run Code Online (Sandbox Code Playgroud)

根据我添加includes(:user)到我的account.rb文件中的指南。我尝试将其添加到索引操作并显示操作,但此警报一次又一次出现。

帐户.rb:

class Account < ApplicationRecord
  belongs_to :user
  belongs_to :customer
end
Run Code Online (Sandbox Code Playgroud)

账户控制器.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
Run Code Online (Sandbox Code Playgroud)

show.html.erb(客户 62..64 行)

<% @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>
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Vas*_*isa 5

您可以再添加一个实例变量,但这会增加controller.action 的大小。第二个选项:

# you didn't a add real code where you define @customer, so it just example
@customer = Customer.includes(accounts: :user).find(params[:customer_id])
Run Code Online (Sandbox Code Playgroud)