已检测到USE渴望加载[Bulllet Gem]

时间:2019-01-18 11:49:01

标签: 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'

根据指南,我在includes(:user)文件中添加了account.rb。我试图将其添加到索引动作并同时显示动作,但是此警报一次又一次地出现。

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(客户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>

我该如何解决?

2 个答案:

答案 0 :(得分:1)

我建议您在html中使用以下

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

在其中,您必须在内部动作中创建实例变量

@accounts = @customer.accounts.includes(:user)

答案 1 :(得分:1)

您可以再添加一个实例变量,但这会增加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])