This project is archived and is in readonly mode.
Count calculations should respect scoped selects
Reported by duncanbeevers | November 5th, 2008 @ 10:02 PM | in 3.0.6
Issuing a count calculation on a scope that explicitly specifies a select option does not respect that select, resulting in incorrect counts.
Comments and changes to this ticket
-
Repository March 7th, 2009 @ 03:55 PM
- State changed from new to resolved
- Tag changed from activerecord, calculations, count, named_scope to activerecord, calculations, count, named_scope, patch
(from [6543426c73fa9ccf3649d7cbacbbb0fda9b6a099]) Ensure calculations respect scoped :select [#1334 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
-
Mislav March 17th, 2009 @ 12:34 PM
- Tag changed from activerecord, calculations, count, named_scope, patch to activerecord, bug, calculations, count, named_scope, patch, regression
This breaks counts on scoped associations:
author.posts.count # => works author.posts.scoped(:conditions => '1=1') # => FAIL
-
Mislav March 17th, 2009 @ 12:38 PM
Uh, the second line in my example should have
count
,size
,empty?
or whatever method at the end to trigger a COUNT query.I also forgot to state the error message: Mysql::Error (bad syntax). There is a join (hmt association) and this SELECT part is generated:
SELECT count(`posts`.*) AS ...
This is invalid SQL, it should be "count(posts.id)".
-
CancelProfileIsBroken March 17th, 2009 @ 04:07 PM
- State changed from resolved to open
- Assigned user set to Pratik
-
Ian Terrell March 17th, 2009 @ 04:12 PM
- Assigned user cleared.
The previous attachment was just a "git diff." Attached is a properly formatted patchfile to add my failing test.
-
Ian Terrell March 17th, 2009 @ 04:20 PM
- Assigned user set to Pratik
Crossed with Mike above. Reassigning.
-
Steven Soroka March 26th, 2009 @ 04:07 PM
this patch caused a bug with scopes and has-many-through's, see card #2310
-
Michael Koziarski June 1st, 2009 @ 02:56 AM
- Milestone cleared.
I'm reverting this in 2-3-stable so we can have a point release.
However I'm leaving this open and targetting 3.0 so we can try and fix the original bug.
-
Wolfram Arnold June 29th, 2009 @ 11:09 PM
I wrote up something similar in #1652
It was suggested to me to implement the :counter_sql option for named_scope instead. ActiveRecord::Base#find supports that too.
-
Pratik July 3rd, 2009 @ 12:50 PM
- Assigned user changed from Pratik to Michael Koziarski
-
Jeremy Kemper August 30th, 2010 @ 04:10 AM
- Milestone cleared.
- Importance changed from to High
-
David Cuddeback February 4th, 2011 @ 08:18 PM
I have a patch that fixes this for the
2-3-stable
branch. I used the tests from Ian Terrell's patch and added one that addresses Mislav's comment. -
Santiago Pastorino February 27th, 2011 @ 03:15 AM
- Milestone changed from 3.0.5 to 3.0.6
-
Jared McFarland April 15th, 2011 @ 12:45 AM
Is this ticket still open? I couldn't reproduce the bug in Rails 3.1
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
- 1349 named scope with group by bug? UPDATE: this issue appears to be related with possible fi...
- 3602 Activerecord ignoring Distinct for Postgres in named scope for count / size Dupe of #1334
- 1334 Count calculations should respect scoped selects (from [6543426c73fa9ccf3649d7cbacbbb0fda9b6a099]) Ensure ...
- 2310 has_many :through + scope + empty? fails in 2.3.2 (but not 2.3.0) This bug was introduced by commit 6543426... Ensure calcu...