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

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

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 3.0.0
    • Component/s: None

      Description

      > 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;

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: