2014-05-07 35 views
0

我認爲是什麼導致此N + 1錯誤是因爲視圖必須呈現集合,每個狀態更新需要進行查詢調用,以顯示它的變量相對於其他狀態更新。N + 1錯誤與視圖

這使得很多查詢。我怎樣才能一次性提供所有這些狀態更新,並且只需從狀態更新類中調用數組來進行這些計算,而不是每次都進入數據庫?

我可能會錯過一些東西,所以任何見解都會很棒!我試過改變我的查詢調用使用包括,約100其他事情。

這是錯誤的子彈是給我:

user: Pablo 
N+1 Query detected 
    StatusUpdate => [:client] 
    Add to your finder: :include => [:client] 
N+1 Query method call stack 
/Users/Nick/Code/Rails/gj/app/models/status_update.rb:27:in `prev' 
/Users/Nick/Code/Rails/gj/app/models/status_update.rb:36:in `weight_change' 
/Users/Nick/Code/Rails/gj/app/models/client.rb:11:in `weight_change' 
/Users/Nick/Code/Rails/gj/app/views/status_updates/show.html.erb:39:in `_app_views_status_updates_show_html_erb__176120213182464780_70358939483460' 

status_updates.rb

def prev 
    prev = self.client.status_updates.where("created_at < ?", self.created_at)[-1] 
    if prev == nil 
    prev = self 
    else 
    prev 
    end 
end   

def weight_change 
    weight_change = prev.total_weight - total_weight 
    cut weight_change 
end 

client.rb

def weight_change(stat_present) 
if stat_present == false 
    0 
elsif stat_present == true 
    self.status_updates.reverse.first.weight_change 
end 
end 

StatusUpdatesController#顯示

def show 
    if status_updates? 
     current_client.id).limit(7).reverse 
     @status_updates = current_client.status_updates.limit(7).reverse 
    end 
    @status_update = current_client.status_updates.new 
    end 

show.html

<% if @status_updates != nil %> 
     <%= render(partial: "status_updates", collection: @status_updates) %> 
    <% else %> 
     <p style="text-align:center">No status updates yet.</p> 
    <% end %>  

_status_updates.html

<tr> 
    <td class="left"><%= status_updates.entry_date.strftime("%m/%d/%y") %></td> 
    <td class="left phase"><%= status_updates.phase %> <a href="#">+</a></td> 
    <td><%= status_updates.total_weight %></td> 
    <td><%= status_updates.weight_change %></td> 
    <td><%= status_updates.lbm_weight %></td> 
    <td><%= status_updates.lbm_change %></td> 
    <td><%= status_updates.total_lbm_change %></td> 
    <td><%= BigDecimal(status_updates.body_fat_pct * 100, 5)%>%</td> 
    <td><%= status_updates.fat_change %></td> 
    <td><%= status_updates.total_fat_change %></td> 
    <td><%= link_to('&times;'.html_safe, status_updates, method: :delete) %></td> 
</tr> 

client.rb

class Client < ActiveRecord::Base 
    belongs_to :trainer 
    has_many :status_updates 

    validates :firstname, :lastname, presence: true 

    def weight_change(stat_present) 
    if stat_present == false 
     0 
    elsif stat_present == true 
     self.status_updates.reverse.first.weight_change 
    end 
    end 

    def fat_change(stat_present) 
    if stat_present == false 
     0 
    elsif stat_present == true 
     self.status_updates.reverse.first.fat_change 
    end 
    end 

    def lbm_change(stat_present) 
    if stat_present == false 
     0 
    elsif stat_present == true 
     self.status_updates.reverse.first.lbm_change 
    end 
    end 

    def bfp_change(stat_present) 
    if stat_present == false 
     0 
    elsif stat_present == true 
     self.status_updates.reverse.first.bfp_change 
    end 
    end 

    def total_weight_change(stat_present) 
    if stat_present == false 
     0 
    elsif stat_present == true 
     self.status_updates.reverse.first.total_weight_change 
    end 
    end 

end 
+1

爲什麼不在創建時將weight_change保存到'status_update'? –

+0

顯示_status_updates – Santhosh

+0

的內容而不是使用'some_array [-1]',如果僅僅是清晰度,最好使用'some_array.last'。 –

回答