0

date_start から date_end までクエリを実行して、特定の製品の在庫数がしきい値を満たしていることを各日付で確認する必要があります。

@inventories = Inventory.where(['product_id IN (?) AND date_available >= ? AND date_available <= ? AND quantity >= ?', @products, @date_start, @date_end, params[:quantity]]).all

This query generates inventories that meet the criterion on any given date. However these conditions need to be met on EACH date for a given product. Thus,

@products = @inventories.map(&:product).uniq

Q1 How do I now extrapolate all available_products (i.e. that quantity >= ? on each date) and

Q2 I need to sum the price for each date of each available_product, in order to sort the view by price

*Edit *

Given a suggested answer, I think it may be wise to provide example data to further clarify product_id date_available quantity price_1 price_2
1 ---- 2014-05-27 ---- 3 ---- 35 ---- 60
1 ---- 2014-05-28 ---- 3 ---- 35 ---- 60
1 ---- 2014-05-29 ---- 3 ---- 25 ---- 50

2 ---- 2014-05-27 ---- 3 ---- 35 ---- 60
2 ---- 2014-05-28 ---- 1 ---- 35 ---- 60
2 ---- 2014-05-29 ---- 3 ---- 35 ---- 60

3 ---- 2014-05-27 ---- 3 ---- 30 ---- 55
3 ---- 2014-05-28 ---- 3 ---- 30 ---- 55
3 ---- 2014-05-29 ---- 3 ---- 30 ---- 55

@price = concatenation of 'price_1' and params[:quantity]

If search params are 2014-05-27..2014-05-29, quantity = 2
I should generate
product 3 with price = 165
product 1 with price 170
product 2 fails because on one of the dates the threshold is not met.

context: rails 3.2.17, postgresql, ruby 1.9.3

4

1 に答える 1

0

ここでクエリを見つけることができます。

@inventories = Inventory.where(['product_id IN (?) AND date_available >= ? AND date_available <= ? AND quantity >= ?', @products, @date_start, @date_end, params[:quantity]]).select("date_available as date, product as product, sum(quantity) as quantity, sum(price) as price")).group('date, product').order('price')

上記のクエリから、毎日と製品の数量と価格を取得できます。

于 2014-05-26T15:10:12.857 に答える