That isn't inherently possible, since the document is referenced.
It's important to remember that GiftCategory
doesn't actually contain a Gift
. Instead, the Gift
records have a field called gift_category_id
. You essentially would need to find Gift
records which have a gifts_unit_count > 0
, then compile a list of their gift_category_id
fields, unique them, and then retrieve those records.
This would do roughly what I've stated above:
gift_category_ids = Gift.where(:gifts_unit_count.gt => 0).map {|g| g.gift_category_id}.uniq
for gift_category_id in gift_category_ids
gift_category = GiftCategory.find(gift_category_id)
# do something with this
end
As far as I know, Mongoid is not willing to do something like this for you. As someone commented above, you may want to consider embedding, which would allow you to query this way, since the fields will be stored in the same document.