This project is archived and is in readonly mode.
Calculations break with multi-column :select argument
Reported by Joshua Krall | June 22nd, 2009 @ 08:43 AM | in 3.0.6
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!
Comments and changes to this ticket
-
CancelProfileIsBroken August 7th, 2009 @ 02:12 PM
- Tag changed from activerecord, calculations, patch to activerecord, bugmash, calculations, patch
-
Michael Koziarski August 8th, 2009 @ 02:54 AM
- 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
-
Jeremy Kemper August 9th, 2009 @ 12:35 AM
- 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.
-
Michael Koziarski August 9th, 2009 @ 12:37 AM
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 -
Jeremy Kemper August 9th, 2009 @ 01:07 AM
- State changed from open to incomplete
-
Jeremy Kemper August 30th, 2010 @ 04:10 AM
- Milestone cleared.
- Importance changed from to
-
Santiago Pastorino February 27th, 2011 @ 03:15 AM
- Milestone changed from 3.0.5 to 3.0.6
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 1229 Add :from scoping respect to Calculations module #2823 is related to this also