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.

Coerce 0 Average Calculation To Float

#1346

Some databases return 0 for a #average calculation. For instance:

Topic.average(:replies_count)

Returns 0 from SQL Server and will bomb when #type_cast_calculated_value is run. I am submitting this patch with high hopes that it can be done in 2.2 release. We are trying to finish the new SQL Server adapter to that release and having all the tests pass would without monkey patching rails would be great!

Cheers.

Reported by Ken Collins · November 7th, 2008 @ 08:35 PM

State: resolved
Milestone: none
Assigned to: josh josh
Importance: none

Activity

  1. josh
    josh
    • State changed from new to resolved
    • Assigned user set to josh

    November 8th, 2008 @ 02:40 AM

  2. Repository
    Repository

    (from [a7f920f674d234f281d2491ebe6d74710a79e663]) If average value from DB is 0, make sure to convert it to a 0.0 float before calling #to_d on it [#1346 Coerce 0 Average Calculation To Float state:resolved]

    Signed-off-by: Joshua Peek josh@joshpeek.com http://github.com/rails/rails/co...

    November 8th, 2008 @ 02:47 AM

  3. Ken Collins
  4. Tarmo Tänav
    Tarmo Tänav
    • State changed from resolved to open

    What's the point of using BigDecimal at all if we're first converting to float?, the bigdecimal is certainly not going to be any more precise than the float it is made from.

    Seems to me that the special case should only be used if the value actually is 0, in which case to_f wouldn't really be needed, one could just use: value && (value == 0 ? 0.0.to_d : value.to_d)

    November 8th, 2008 @ 11:02 PM

  5. Ken Collins
    Ken Collins

    That looks just fine to me. I'll put together another patch.

    November 10th, 2008 @ 05:17 AM

  6. josh
  7. Repository
    Repository
    • State changed from open to resolved

    (from [335a31524055c7dd79618ea79b3c18d827e25d3d]) Add simple case when DB calculations returns 0 instead of 0.0 [#1346 Coerce 0 Average Calculation To Float state:resolved] http://github.com/rails/rails/co...

    November 10th, 2008 @ 08:17 PM

  8. Ken Collins
    Ken Collins

    Thanks again! My Monday was crushed with bugs from co-workers. Cheers!

    November 11th, 2008 @ 01:54 PM