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 method doesn't work with specified conditions

#2395

AssociationCollection#count method won't take conditions in arguments into account if there is finder_sql or counter_sql. e.g.

class User has_many :photos, :finder_sql => '...' end

some_user.photos.count(:conditions => ['photos.created_at > ?', Time.today]) # all user photos will be returned, not only those created today

I wrote a patch to fix this (compatible with older implementation)

Reported by Jan Xie · April 2nd, 2009 @ 08:15 AM

State: resolved
Milestone: 3.x
Assigned to: José Valim José Valim
Importance: none

Activity

  1. Jan Xie
    Jan Xie
    • Assigned user set to Jeremy Kemper

    May 11th, 2009 @ 09:11 AM

  2. Jeremy Kemper
    Jeremy Kemper

    We avoid touching custom finder_sql since appending " AND conditions" may just break the query.

    The patch has a passing test case. Does it fail for other uses of finder_ or counter_sql?

    May 11th, 2009 @ 07:34 PM

  3. Jan Xie
    Jan Xie

    Though I think it will work at most time, yes it will break some finder_sql. To make it works in all cases, a mini sql parser need to be introduced, which doesn't look like a good option :)

    May 12th, 2009 @ 04:19 AM

  4. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  5. Dan Pickett
    Dan Pickett
    • Tag changed from active_record, conditions, count to active_record, bugmash, conditions, count

    May 9th, 2010 @ 06:00 PM

  6. Gavin Stark
    Gavin Stark

    If modifications to the conditions of an association using a finder_sql are not allowed, then they should not be silently ignored. Perhaps an exception could be raised which would at least tell the developer something rather than executing and returning unexpected results?

    May 16th, 2010 @ 12:40 AM

  7. Rohit Arondekar
    Rohit Arondekar

    Any updates to this ticket?

    June 23rd, 2010 @ 03:39 AM

  8. Neeraj Singh
    Neeraj Singh
    • Importance changed from to

    After this ticket was created Jeremy said "We avoid touching custom finder_sql since appending " AND conditions" may just break the query."

    +1 for that.

    I would agree with Gavin that raising an exception should be a reasonable compromise.

    Jeremy any thoughts on how we should proceed on this one.

    July 1st, 2010 @ 06:31 PM

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

    Failing fast -- raising an exception -- would be great. Patch + test welcome!

    July 1st, 2010 @ 06:48 PM

  10. Neeraj Singh
    Neeraj Singh
    • Tag changed from active_record, bugmash, conditions, count to active_record, bugmash, conditions, count, patch

    Attached is patch with test.

    July 2nd, 2010 @ 02:45 AM

  11. Neeraj Singh
    Neeraj Singh
    • Assigned user changed from Jeremy Kemper to José Valim

    July 13th, 2010 @ 08:04 PM

  12. Repository
    Repository
    • State changed from open to resolved

    (from [edb5401039ee15c37b201244c5dbf660bed51fb4]) count method should not take options if it is operated on has_many association which has finder_sql or counter_sql

    [#2395 count method doesn't work with specified conditions state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/edb5401039ee15c37b201244c5dbf6...

    July 13th, 2010 @ 09:05 PM