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.

Count calculations should respect scoped selects

#1334

Issuing a count calculation on a scope that explicitly specifies a select option does not respect that select, resulting in incorrect counts.

Reported by duncanbeevers · November 5th, 2008 @ 10:02 PM

State: open
Milestone: 3.0.6
Assigned to: Michael Koziarski Michael Koziarski
Importance: High

Activity

  1. Repository
    Repository
    • 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...

    March 7th, 2009 @ 03:55 PM

  2. Mislav
    Mislav
    • 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
    

    March 17th, 2009 @ 12:34 PM

  3. Mislav
    Mislav

    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)".

    March 17th, 2009 @ 12:38 PM

  4. Ian Terrell
    Ian Terrell

    Attached is a patch that includes a failing test case.

    March 17th, 2009 @ 03:51 PM

  5. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from resolved to open
    • Assigned user set to Pratik

    March 17th, 2009 @ 04:07 PM

  6. Ian Terrell
    Ian Terrell
    • Assigned user cleared.

    The previous attachment was just a "git diff." Attached is a properly formatted patchfile to add my failing test.

    March 17th, 2009 @ 04:12 PM

  7. Ian Terrell
    Ian Terrell
    • Assigned user set to Pratik

    Crossed with Mike above. Reassigning.

    March 17th, 2009 @ 04:20 PM

  8. Miha Filej
  9. Steven Soroka
    Steven Soroka

    this patch caused a bug with scopes and has-many-through's, see card #2310

    March 26th, 2009 @ 04:07 PM

  10. Michael Koziarski
    Michael Koziarski
    • 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.

    June 1st, 2009 @ 02:56 AM

  11. Wolfram Arnold
    Wolfram Arnold

    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.

    June 29th, 2009 @ 11:09 PM

  12. Pratik
    Pratik
    • Assigned user changed from Pratik to Michael Koziarski

    July 3rd, 2009 @ 12:50 PM

  13. Jeremy Kemper
    Jeremy Kemper
    • Milestone cleared.
    • Importance changed from to High

    August 30th, 2010 @ 04:10 AM

  14. Jeremy Kemper
  15. Santiago Pastorino
  16. David Cuddeback
    David Cuddeback

    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.

    February 4th, 2011 @ 08:18 PM

  17. Santiago Pastorino
  18. Santiago Pastorino
    Santiago Pastorino
    • Milestone changed from 3.0.5 to 3.0.6

    February 27th, 2011 @ 03:15 AM

  19. Jared McFarland
    Jared McFarland

    Is this ticket still open? I couldn't reproduce the bug in Rails 3.1

    April 15th, 2011 @ 12:45 AM