Как вызвать метод в интерфейсе запроса выбранной активной записи из другого метода?
# return quantity of boxes
def amount_boxes(order_id)
amount_boxes = OrderProduct.where('order_products.order_id' => order_id).where('order_products.box IS TRUE').pluck("SUM(order_products.quantity)").first
if amount_boxes.nil?
0
else
amount_boxes
end
end
# return amount boxes accumulated.
def amount_boxes_accumulated(user_id, date_delivery)
quantity = OrderHistory.joins(order: [:load, :order_products])
.select("DISTINCT order_histories.order_id, #{amount_boxes('order_histories.order_id')} as amount_boxes")
.where('loads.delivery_date' => date_delivery, 'order_histories.user_id' => user_id)
.where('order_products.box IS TRUE')
.where.not(end: nil)
# time_total = OrderHistory.select("SUM(t.quantity) as amount_boxes").from(quantity, :t)
end
В методе amount_boxes_accumulated я делаю .. # {amount_boxes ('order_histories.order_id')} в качестве amount_boxes .. но это не работает.
Каков был бы жизнеспособный способ для этого работать?
результат того, что там, не генерирует ошибки, толькопоявляется 0 в запросе .. как ...
SELECT DISTINCT order_histories.order_id, 0 as amount_boxes FROM `order_histories` INNER JOIN `orders` ON `orders`.`id` = `order_histories`.`order_id` INNER JOIN `loads` ON `loads`.`id` = `orders`.`load_id` INNER JOIN `order_products` ON `order_products`.`order_id` = `orders`.`id` WHERE `loads`.`delivery_date` = '2019-09-25 00:00:00' AND `order_histories`.`user_id` = 931135 AND (order_products.box IS TRUE) AND `order_histories`.`end` IS NOT NULL