Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] Check and immediately return value from an average if it's already a BigDecimal

#4514

The return value from an average calculation is assumed to respond to #to_d but the value may already be a BigDecimal to start with. This patch adds a check for that case and returns the value as-is.

Reported by Brian Lopez · April 30th, 2010 @ 11:05 PM

State: committed
Milestone: 3.0.2
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh

    +1

    Looks good to me.

    May 1st, 2010 @ 05:20 AM

  2. Andrew White
    Andrew White

    I don't think the value is ever a BigDecimal, as it doesn't have a to_d method so would raise a NoMethodError if it ever was.

    May 3rd, 2010 @ 03:18 PM

  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 2.3.6
    • State changed from new to open

    It is when the driver returns typecasted results, like mysql2.

    Brian, this whole method should probably return self for non-Strings.

    May 3rd, 2010 @ 05:25 PM

  4. Brian Lopez
    Brian Lopez

    Was hoping to fix and repost the patch this weekend but didn't end up having time. I'll try and get it up today or tomorrow

    May 3rd, 2010 @ 07:11 PM

  5. Brian Lopez
    Brian Lopez

    Here's the updated patch, still looking into test failures to see if they're from this or something else

    May 3rd, 2010 @ 10:55 PM

  6. Brian Lopez
    Brian Lopez

    Ok, here's the patch with updated and passing tests (based on master)

    May 4th, 2010 @ 09:30 AM

  7. Jeremy Kemper
    Jeremy Kemper

    Hey Brian, this changes behavior on nil results too, though. Think it should be confined to just letting pre-typecasted values through.

    May 4th, 2010 @ 07:01 PM

  8. Brian Lopez
    Brian Lopez

    Was worried that wouldn't be desired - in our short chat on IRC about it you mentioned returning 0 for nil which is why I put it in there.
    It does make sense to me that a sum/avg/count should return a Numeric in all cases, but then again it does seem a better fit to return exactly what the db is giving us (even null).

    Updated patch coming soon...

    May 4th, 2010 @ 07:10 PM

  9. Brian Lopez
  10. Repository
    Repository
    • State changed from open to committed

    May 4th, 2010 @ 07:58 PM

  11. Brian Lopez
  12. Repository
  13. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.
    • State changed from committed to open

    This break test_should_average_field test.
    Patchs for master and 2-3-stable added.

    May 10th, 2010 @ 07:45 AM

  14. Santiago Pastorino
    Santiago Pastorino

    A better master patch here. I will redo the one for 2-3-stable later

    May 12th, 2010 @ 05:50 AM

  15. Santiago Pastorino
  16. Santiago Pastorino
  17. Repository
    Repository
    • State changed from open to committed

    (from [ad73a3aec4665d8a44060640e51075fd732c33a1]) type_cast_calculated_value refactor: value is never a Fixnum here. Fix test since SQLite returns Float.

    [#4514 [PATCH] Check and immediately return value from an average if it's already a ... state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/ad73a3aec4665d8a44060640e51075...

    May 12th, 2010 @ 06:30 AM

  18. Repository
  19. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM

  20. Andreas Mayer