我有这个问题:
Order.joins(cart: { cart_items: :product })
.group("products.name", "cart_items.amount")
.count
.map { |a, b| { name: a[0], amount: a[1] * b} }
它返回如下内容:
[{:name=>"Lorem", :amount=>1},
{:name=>"Lorem", :amount=>2},
{:name=>"Foo", :amount=>1}]
问题是因为如果我有2个cart_items使用相同的产品,它会返回2个值,您可以在产品“Lorem”中看到
我期待这样的事情:
[{:name=>"Lorem", :amount=>3},
{:name=>"Foo", :amount=>1}]
答案 0 :(得分:1)
这就是你想要的:
SELECT * FROM `app_news_newstag` GROUP BY `news_id` HAVING `tag_id` IN ('1', '4', '5') AND `tag_id` NOT IN ('2')