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.

Calculations break with multi-column :select argument

#2823

I ran into a problem with geokit and will_paginate... that I tracked down to a bad interaction between the :select option and calculation methods.

It boils down to a simple case that looks like this:

Account.scoped(:select => "credit_limit, COS(credit_limit) as cosine_of_credit_limit").count

This case demonstrates what geokit adds to the :select argument, but it also fails if you do something as simple as :select=>'a,b'

I'm uploading a new patch to fix this, by splitting the column on commas and taking the first column for the COUNT() directive. So, :select=>'a,b' turns into "SELECT COUNT(a) as count_a ..."

All tests pass, but take a look and see if you find anything wrong here. Thanks!

Reported by Joshua Krall · June 22nd, 2009 @ 08:43 AM

State: incomplete
Milestone: 3.0.6
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activerecord, calculations, patch to activerecord, bugmash, calculations, patch

    August 7th, 2009 @ 02:12 PM

  2. Michael Koziarski
    Michael Koziarski
    • Assigned user set to Michael Koziarski
    • Tag changed from activerecord, bugmash, calculations, patch to activerecord, calculations, patch
    • Milestone cleared.

    We can't just take the first column specified, while it works for your example it's not 'good enough' in general. Instead we need a way to fix it permanently

    I'd question why we're even taking the :select key from find scopes. I've attached a patch that takes it from :count scopes instead.

    I've attached a patch for this and would appreciate your feedback. Your fix would look like:

        Account.with_scope(:find=>{:select=>"COS(id), SIN(id)"}, :count=>{:select=>"id"}) do
    

    August 8th, 2009 @ 02:54 AM

  3. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to open

    This is messy indeed. I'd almost see an error raised than let this slip through or get hacked around.

    Michael, that fix works for count, but not the other calculations.

    August 9th, 2009 @ 12:35 AM

  4. Michael Koziarski
    Michael Koziarski

    OK, I agree on the error case, let's just raise an exception in the
    event that the :select scope doesn't map onto the column_name

    August 9th, 2009 @ 12:37 AM

  5. Jeremy Kemper
    Jeremy Kemper
    • State changed from open to incomplete

    August 9th, 2009 @ 01:07 AM

  6. Jeremy Kemper
    Jeremy Kemper
    • Milestone cleared.
    • Importance changed from to

    August 30th, 2010 @ 04:10 AM

  7. Jeremy Kemper
  8. Santiago Pastorino
  9. Santiago Pastorino
  10. Santiago Pastorino
    Santiago Pastorino
    • Milestone changed from 3.0.5 to 3.0.6

    February 27th, 2011 @ 03:15 AM