[rails 2.3.12] named_scope:
named_scope :order_by_price, lambda {{:joins => :variants, :group => "products.id", :order => "MAX(price)"}}
控制台:
1.
> Product.order_by_price.size
=> 21
2.
> p = Product.order_by_price
> p.size
=> 4
sql查询:
1.
SELECT count(*) AS count_all FROM `products` INNER JOIN `variants` ON variants.product_id = products.id
2.
SELECT `products`.* FROM `products` INNER JOIN `variants` ON variants.product_id = products.id GROUP BY products.id ORDER BY MAX(price)
我使用will_paginate进行分页。在这种情况下,total_entries值为21,页数基于此,尽管只有4个产品......
任何想法如何让它正常工作?
修改
一般情况下,我必须在调用Product.count时包括group_by ...如何?
答案 0 :(得分:1)
没有答案,但我找到了解决方案。也许它对其他人也有用。我只需要重新定义计数,选择不同的product_id:
def self.count(*args)
super(args, {:select => "(products.id)", :distinct => true})
end