我正在尝试购买至少购买两件商品的顾客。关联非常简单,但我在测试中没有得到任何结果。也许另一组眼睛可以看到我所缺少的东西。
# customer.rb
class Customer < Person
has_many :orders, foreign_key: 'person_id'
has_many :items, through: :orders
# This is what I'm needing help with:
def self.min_2_orders
joins(:items).group('items_orders.item_id').having('COUNT(items_orders.item_id) >= ?', 2)
end
end
-
# order.rb
class Order < ActiveRecord::Base
belongs_to :customer, foreign_key: 'person_id'
has_and_belongs_to_many :items
end
-
# item.rb
class Item < ActiveRecord::Base
has_and_belongs_to_many :orders
end
-
# customer_spec.rb
it "finds customers who have ordered a min of 2 items" do
@order1, @order2 = FactoryGirl.create_list(:order, 2) #returns 2 created orders
@order2.items << FactoryGirl.create_list(:item, 2) #returns 2 created items
@customer1 = @order2.customer
@customer2 = @order1.customer
## Debug tests
Order.count.should == 2 #pass
Customer.count.should == 2 #pass
Item.count.should == 2 #pass
@order_min.items.count.should == 2 #pass
@customer1.items.count.should == 2 #pass
puts Order.all.to_yaml # debug. see below
puts Customer.all.to_yaml # debug. see below
puts Item.all.to_yaml # debug. see below
# End debugging
# The final test
Customer.min_2_orders.should == [@customer1] # FAIL. returns []
end
以下是调试日志(sql查询和YAML db数据):
"SELECT 'people'.* FROM 'people'
INNER JOIN 'orders' ON 'orders'.'person_id' = 'people'.'id'
INNER JOIN 'items_orders' ON 'items_orders'.'order_id' = 'orders'.'id'
INNER JOIN 'items' ON 'items'.'id' = 'items_orders'.'item_id'
WHERE 'people'.'type' IN ('Customer')
GROUP BY items_orders.item_id
HAVING COUNT(items_orders.item_id) >= 2"
---
- !ruby/object:Order
attributes:
id: 1
person_id: 1
created_at: 2013-06-18 16:42:17.558683000 Z
updated_at: 2013-09-17 16:42:17.597082000 Z
- !ruby/object:Order
attributes:
id: 2
person_id: 2
created_at: 2013-09-17 16:42:17.600267000 Z
updated_at: 2013-09-17 16:42:17.600267000 Z
---
- !ruby/object:Customer
attributes:
id: 1
first_name: Wes
type: Customer
created_at: 2013-09-17 16:42:17.565677000 Z
updated_at: 2013-09-17 16:42:17.565677000 Z
- !ruby/object:Customer
attributes:
id: 2
first_name: Wes
type: Customer
created_at: 2013-09-17 16:42:17.599013000 Z
updated_at: 2013-09-17 16:42:17.599013000 Z
---
- !ruby/object:Item
attributes:
id: 1
name: Product 1
created_at: 2013-09-17 16:42:17.632347000 Z
updated_at: 2013-09-17 16:42:17.632347000 Z
- !ruby/object:Item
attributes:
id: 2
name: Product 2
created_at: 2013-09-17 16:42:17.633920000 Z
updated_at: 2013-09-17 16:42:17.633920000 Z
答案 0 :(得分:1)
由于您按item_id
进行分组,因此每item_id
只能获得一条记录,因此您永远不会有count(item_id)
大于{1}的记录。您应该按people.id
进行分组。
在相关点上,MySQL的独特之处在于:
MySQL扩展了GROUP BY的使用,以便选择列表可以引用 未在GROUP BY子句中命名的非聚合列
如http://dev.mysql.com/doc/refman/5.1/en/group-by-extensions.html
中所述