タイトルが不明確な場合は申し訳ありません-この質問の言い方がよくわかりません。
Rails 3.2、postgresql、および ruby 1.9.3 を使用しています。
私は 2 つのモデルを持っています: 多くの項目を持つ注文。注文には items_count の counter_cache があり、アイテムにはステータスがあります。日付範囲内の注文でステータスが「in_progress」のアイテムを検索する必要があります。
私は現在、検索フォームに Ransack を使用しています。最初は、主に注文が必要だったので問題ありませんでした。次に、オーダーのアイテムの数を取得する必要がありました。これは、items_count のオーダーで counter_cache の合計を使用して行いました。しかし今、その合計を in_progress のアイテムだけに制限する必要があります。
@search = Order.order("orders.created_at desc").search(params[:q])
# This retrieves all orders within the search params, and
# paginates them with kaminari
@orders = @search.result(distinct: true).page(params[:page]).per(params[:per])
# Here is use the original search again because my total
# needs to exclude estimates, and then sum the items
@items_total = @search.result(distinct: true).where(estimate: false)
.sum(:items_count)
これで合計が得られますが、ちょうど の合計が必要ですitems.status='in_progress'
。私は、SQL の代わりに Ruby を使用して、既に持っている注文をループし、in_progress アイテムを手動で追加することに傾いています。しかし、これを処理するためのより良い方法があるかもしれないと思いました。
ありがとう!