0

我有一个盒子,它有很多隔间,其中包含许多属于一个类别的项目。项目是多态的(:itemable),因为它们可以属于没有隔间的盒子——这可能是我的困难。

我可以在 Box 控制器中为 DB 中的所有 Box 调用所有 Items:

@category_items = Item.includes(:category).group("categories.name").sum("quantity * value")

但是如何将这些过滤到仅属于当前 Box id 的那些?

4

2 回答 2

0

尝试

compartment_ids = %(SELECT id FROM compartments WHERE box_id = :box_id)

@category_items = Item.includes(:category).where("(itemable_type = 'box' AND itemable_id = :box_id) OR (itemable_type = 'compartment' AND itemable_id IN (#{compartment_ids}))", :box_id => box_id).group("categories.name").sum("quantity * value")
于 2012-09-20T16:41:24.407 回答
0

尝试以下方式:

@category_items= Item.includes(:category).filter(params[:box], [:box_id,:compartment_id, "categories.name"])
于 2012-09-20T18:04:22.570 回答