Count calculations should respect scoped selects #583

Closed
lighthouse-import opened this Issue May 16, 2011 · 18 comments

Projects

None yet

1 participant

@lighthouse-import

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/1334
Created by duncanbeevers - 2011-02-19 09:28:30 UTC

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

@lighthouse-import

Imported from Lighthouse.
Comment by Repository - 2009-03-07 15:55:06 UTC

(from [6543426]) Ensure calculations respect scoped :select [#1334 state:resolved]

Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/6543426c73fa9ccf3649d7cbacbbb0fda9b6a099

@lighthouse-import

Imported from Lighthouse.
Comment by Mislav - 2009-03-17 12:34:02 UTC

This breaks counts on scoped associations:

author.posts.count  # => works
author.posts.scoped(:conditions => '1=1')  # => FAIL
@lighthouse-import

Imported from Lighthouse.
Comment by Mislav - 2009-03-17 12:38:21 UTC

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

@lighthouse-import

Imported from Lighthouse.
Comment by Ian Terrell - 2009-03-17 15:51:12 UTC

Attached is a patch that includes a failing test case.

@lighthouse-import

Imported from Lighthouse.
Comment by Ian Terrell - 2009-03-17 16:12:11 UTC

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

@lighthouse-import

Imported from Lighthouse.
Comment by Ian Terrell - 2009-03-17 16:20:18 UTC

Crossed with Mike above. Reassigning.

@lighthouse-import

Imported from Lighthouse.
Comment by Miha Filej - 2009-03-18 17:34:54 UTC

Workaround: #2189

@lighthouse-import

Imported from Lighthouse.
Comment by Steven Soroka - 2009-03-26 16:07:17 UTC

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

@lighthouse-import

Imported from Lighthouse.
Comment by Michael Koziarski - 2009-06-01 01:56:46 UTC

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.

@lighthouse-import

Imported from Lighthouse.
Comment by Wolfram Arnold - 2009-06-29 22:09:47 UTC

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.

@lighthouse-import

Imported from Lighthouse.
Comment by Jeremy Kemper - 2010-08-30 03:10:29 UTC

[bulk edit]

@lighthouse-import

Imported from Lighthouse.
Comment by Jeremy Kemper - 2010-10-15 22:01:29 UTC

[bulk edit]

@lighthouse-import

Imported from Lighthouse.
Comment by Santiago Pastorino - 2010-11-15 21:04:48 UTC

[bulk edit]

@lighthouse-import

Imported from Lighthouse.
Comment by David Cuddeback - 2011-02-04 20:18:11 UTC

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.

@lighthouse-import

Imported from Lighthouse.
Comment by Santiago Pastorino - 2011-02-12 21:58:20 UTC

[bulk edit]

@lighthouse-import

Imported from Lighthouse.
Comment by Santiago Pastorino - 2011-02-27 03:15:37 UTC

[bulk edit]

@lighthouse-import

Imported from Lighthouse.
Comment by Jared McFarland - 2011-04-14 23:45:07 UTC

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

@lighthouse-import

Attachments saved to Gist: http://gist.github.com/969913

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment