2016-11-22 1 views
0

統計ページがあり、現在はItems BoughtItems Soldというテーブルがあります。 Items Boughtテーブルは正常に動作していますが、Items Soldテーブルで問題が発生しています。私のDBにはブール値があり、SOLDです。TRUEと記されているすべての項目にカウントが表示されます。現在の.whereステートメントにはdateステートメントもあります。私はそれらを両方ともエラーに組み込む方法がわかりません。Railsコントローラで.whereを使用する方法

items_controller:

def statistics 
    @items = Item.all 
    @items_day = Item.all.where('created_at >= ? AND created_at <= ?', Time.zone.now.beginning_of_day, Time.zone.now.end_of_day) 
    @items_week = Item.all.where('created_at >= ? AND created_at <= ?', Time.zone.now.beginning_of_week, Time.zone.now.end_of_week) 
    @items_month = Item.all.where('created_at >= ? AND created_at <= ?', Time.zone.now.beginning_of_month, Time.zone.now.end_of_month) 

    @items_sold = Item.all.where(:sold => true) 
    @items_sold_day = Item.all.where(:sold => true 'created_at >= ? AND created_at <= ?', Time.zone.now.beginning_of_day, Time.zone.now.end_of_day) 
    @items_sold_week = Item.all.where(:sold => true 'created_at >= ? AND created_at <= ?', Time.zone.now.beginning_of_week, Time.zone.now.end_of_week) 
    @items_sold_month = Item.all.where(:sold => true 'created_at >= ? AND created_at <= ?', Time.zone.now.beginning_of_month, Time.zone.now.end_of_month) 
end 

statistics.html.erb

<h3 id="subtitle">Items Bought</h3> 
<table align="center" style="width: 95%" class="table table-striped table-bordered"> 
    <thead> 
    <tr> 
     <th>Total</th> 
     <th>Today</th> 
     <th>Week</th> 
     <th>Month</th> 
    </tr> 
    </thead> 

    <tbody> 
    <tr> 
     <td><%= number_with_delimiter(@items.count) %></td> 
     <td><%= @items_day.count %></td> 
     <td><%= @items_week.count %></td> 
     <td><%= @items_month.count %></td> 
    </tr> 
    </tbody> 
</table> 

<h3 id="subtitle">Items Sold</h3> 
<table align="center" style="width: 95%" class="table table-striped table-bordered"> 
    <thead> 
    <tr> 
     <th>Total</th> 
     <th>Today</th> 
     <th>Week</th> 
     <th>Month</th> 
    </tr> 
    </thead> 

    <tbody> 
    <tr> 
     <td><%= number_with_delimiter(@items_sold.count) %></td> 
     <td><%= @items_sold_day.count %></td> 
     <td><%= @items_sold_week.count %></td> 
     <td><%= @items_sold_month.count %></td> 
    </tr> 
    </tbody> 
</table> 
+1

まず '項目.all.where'それはちょうど 'Item.where'でなければなりません。次のことは、' count'を多くの場所で使っていることです。 '@ items_sold.count' sho uldはあなたのために仕事をしますか? – Deep

+1

.allを削除して配列にすると、おそらくオフにして、できるだけアクティブな関係にしておきたいと思うでしょう。 – Doon

答えて

3

することはできチェーンいくつかのwhere文:

あなたがする必要はありませんすべての
Item.where(:sold => true).where('created_at >= ? AND created_at <= ?', Time.zone.now.beginning_of_day, Time.zone.now.end_of_day) 
+0

それは素晴らしい作品です!私が答えとしてそれを受け入れるとすぐに、私はします。再度、感謝します! –

関連する問題