Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-566

collection/context sum method should delegate to to_a result, and act as proc

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.0
    • Affects Version/s: None
    • Component/s: None

      > Post.first.comments.sum(:helpfulness)
      NoMethodError Exception: undefined method `+' for #<Comment:0x1083d5848>
      > Post.first.comments.to_a.sum(&:helpfulness)
      17

      The first should work exactly like the second, except that the first should make one SUM query to the database, while the second loops over each comment and does numeric math.

      In the end, they should both return the equiv of the following SQL that Rails generates:

      SELECT SUM(usefulness) FROM comments WHERE post_id = 1;

            Assignee:
            durran Durran Jordan
            Reporter:
            KieranP Kieran Pilkington [X]
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: