-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
None
EDIT: see below comment.
class Account include Mongoid::Document has_many :line_items, class_name:"OpportunityLineItem" def balance line_items.only(:amount).sum(:amount) || 0 end end
When in the console I can see that there is a line item:
[33] pry(#<Opportunity>)> act.line_items => [#<OpportunityLineItem _id: 508817d8913f490a66000001, _type: nil, created_at: 2012-10-24 16:31:20 UTC, updated_at: 2012-10-24 16:31:20 UTC, account_id: "508817e7913f490a66000003", timestamp: nil, amount: 300.0, description: "", type: nil, units: 0, nullified_by_adjustment: false, video_participant_id: nil, opportunity_id: nil, opportunity_channel_participant_id: nil, corresponding_line_item_ids: []>] [34] pry(#<Opportunity>)>
However, calling #count on this relation object returns 0. Calling #size correctly returns 1, but it appears that the #sum aggregation uses #count to do its magic.
Needing to work around this, I tried calling act.line_items.all to get back a Mongoid::Criteria object which I know to respond to #count, but in this case even that returns 0.
Is this is bug, or am I just using this relation in a way it shouldn't be used? Using an older version of Mongoid 3 with an earlier version of my app these aggregations seem to work without issue (and in that case I'm also using a class name that is different from the relation name).
Note: if you are looking at this thinking "man, he really shouldn't be using MongoDB," I completely agree and I'm getting my app ready to use Postgres. Baby steps.