我知道我的问题是微不足道的,但我需要一些帮助。
我有两个具有关系的 mongoid 类has_many
:
class Container
...
has_many: items
...
end
class Item
...
field :date, type => Date
belongs_to: container
...
end
我需要在特定日期有物品的所有容器。
这是无法按预期工作的范围。它返回在给定日期有物品的容器和没有物品的容器:
class Container
...
scope :with_items_on, ->(date){ where(:_id.in => Item.on(date).only(:container_id).distinct(:container_id)) }
...
end
.on(date)
是正确返回给定日期的所有项目的范围。谢谢。