This project is archived and is in readonly mode.

#2802 ✓stale

fixed counter_sql from finder_sql to work with subselects

Reported by Kane | June 14th, 2009 @ 06:27 PM | in 3.x

This patch allows to use subselects in the finder_sql without specifing the counter_sql which was impossible because the regex was too greedy. The patch fixes this issue for has_many, has_many :through and has_and_belongs_to_many.
Tests included.

Comments and changes to this ticket

  • Pratik

    Pratik June 22nd, 2009 @ 10:04 AM


    The patch doesn't apply cleanly. Also, could you please fix your email in the patch ?

    git config --global "Your Name"
    git config --global
  • Kane

    Kane July 21st, 2009 @ 10:55 PM

    • Assigned user set to “Pratik”
  • Pratik

    Pratik July 29th, 2009 @ 06:56 PM

    • State changed from “new” to “incomplete”

    Hey Kane,

    The tests fail on sqlite3. I think the test should be skipped for adapters not supporting subqueries.


  • Kane

    Kane July 29th, 2009 @ 09:36 PM

    are there adapters which dont support subqueries?

    I changed the sql, now works with sqlite3 and should not be adapter specific.

    hope this is the final version^^

  • Pratik

    Pratik July 30th, 2009 @ 01:33 PM

    The test still fails for me on sqlite3.

      1) Error:
    ActiveRecord::StatementInvalid: SQLite3::SQLException: near ")": syntax error: SELECT COUNT(*) FROM companies LIMIT 1) IS NOT NULL;
        ./cases/../../lib/active_record/connection_adapters/abstract_adapter.rb:214:in `log'
        ./cases/../../lib/active_record/connection_adapters/sqlite_adapter.rb:168:in `execute_without_query_record'
        ./cases/../../lib/active_record/connection_adapters/sqlite_adapter.rb:413:in `catch_schema_changes'
        ./cases/../../lib/active_record/connection_adapters/sqlite_adapter.rb:168:in `execute_without_query_record'
        ./cases/helper.rb:41:in `execute'
        ./cases/../../lib/active_record/connection_adapters/sqlite_adapter.rb:316:in `select'
        ./cases/../../lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
        ./cases/../../lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
        ./cases/../../lib/active_record/connection_adapters/abstract/database_statements.rb:13:in `select_one'
        ./cases/../../lib/active_record/connection_adapters/abstract/database_statements.rb:19:in `select_value'
        ./cases/../../lib/active_record/base.rb:956:in `count_by_sql'
        ./cases/../../lib/active_record/associations/association_collection.rb:169:in `count'
        cases/associations/has_many_associations_test.rb:36:in `test_counting_from_finder_sql_with_subselect'
        ./cases/../../lib/../../activemodel/lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `__send__'
        ./cases/../../lib/../../activemodel/lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `run'
  • Kane

    Kane August 1st, 2009 @ 01:11 PM

    strange, i only get this error without the fix.

  • Kane

    Kane August 24th, 2009 @ 11:54 PM

    i rebased and removed a conflict.

    also 2 other developers tried my patch and they got no error.
    i have no idea why its failing on your machine.

    tested on:
    windows xp, ruby 1.8.6, sqlite3-ruby 1.2.5 with sqlite 3.5.6
    ubuntu, ruby 1.9.1, sqlite3-ruby 1.2.4 with sqlite 3.6.10

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Kane

    Kane June 22nd, 2010 @ 07:36 PM

    this is still an issue in edge..

  • Kane

    Kane June 22nd, 2010 @ 07:36 PM

    • Assigned user changed from “Pratik” to “Jeremy Kemper”
  • Neeraj Singh

    Neeraj Singh July 2nd, 2010 @ 03:39 AM

    • Importance changed from “” to “”

    @Kane can you provide an example of "subselects in the finder_sql" so I could fully understand the problem this patch is solving. If I understand it then I can write a test for your patch.


  • Kane

    Kane August 2nd, 2010 @ 06:24 PM

    SELECT * FROM articles WHERE id IN (SELECT article_id FROM comments WHERE text LIKE '%test%');

  • rails

    rails February 26th, 2011 @ 12:00 AM

    • State changed from “incomplete” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails February 26th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

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=""></a>