"Error: nothing to repeat
.link/test.getTest@file:www/js/directives.js:336:28
.link/<@file:www/js/directives.js:357:76"
我需要列出所有产品及其order_item的总和及其total_price的总和
class Product < ActiveRecord::Base
belongs_to :category
has_many :order_items, dependent: :destroy
end
class OrderItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
end
答案 0 :(得分:1)
尝试此查询
select a.name,sum(quantity) as quantity ,sum(total_price) as total_price from
Product a
join OrderItem b on a.id=b.product_id
Group by a.name
答案 1 :(得分:1)
select p.name, sum(o.quantity) as quantity, sum(o.total_price) as total_price
from Product p
join OrderItem o on p.id = o.product_id
group by p.name
答案 2 :(得分:1)
尝试使用此活动记录查询。只需验证您可以使用的列,表名和关联,如:
OrderItem.joins(:product).select("products.name as name,sum(total_price) as total_price , sum(quantity) as total_quantity").group("order_items.product_id").as_json
答案 3 :(得分:0)
p = Product.first
p.order_items.sum(:quantity)
我希望这也有帮助