我有一個GiftCategory
模型:Mongoid:凡has_many關聯具有一定的領域對象的查詢值
class GiftCategory
include Mongoid::Document
field :gifts_count, type: Integer
has_many :gifts, :inverse_of => :gift_category
end
而且我有一個Gift
模型:
class Gift
include Mongoid::Document
field :gift_units_count, type: Integer
has_many :gift_units, :inverse_of => :gift
belongs_to :gift_category, :inverse_of => :gifts, :counter_cache => true
after_save :update_counter
def update_counter
self.gift_category_id_change.each do |e|
GiftCategory.reset_counters(e, :gifts) unless e.nil?
end
end
end
的update_counter
方法可以讓我保持的數有多少個Gift
對象屬於GiftCategory
。這樣我可以查詢僅GiftCategory
對象有一些Gift
對象:
GiftCategory.where(:gifts_count.gt => 0)
但你可以看到,一個Gift
有gift_units_count
領域也是如此。該字段保持Gift
的可用數量單位的計數。如何查詢GiftCategory
具有Gift
對象的對象gift_units_count > 0
?
我認爲解決方案可能類似here,但我無法靠近自己。
如果您需要一個條件:'GiftCategory.where(:id.in => gift_category_ids)' – drinor