This project is archived and is in readonly mode.

#4275 ✓stale
Nick @ Reenhanced

Scopes should not be used when association has :finder_sql

Reported by Nick @ Reenhanced | March 26th, 2010 @ 04:10 PM

Compatibility with legacy data requires me to use a :finder_sql

It appears that when the finder_sql is used however, the normal methods you can call on associations are broken.

A controller requires a limit of 3 items from an association, attempting to use #all(:limit => 3) causes the issue below.

For now, I've worked around this by treating the association as an Enumerable and use #take(3) instead.

class Company < ActiveRecord::Base
  has_many :stats, :finder_sql => 'SELECT from_table.* 
                        FROM gs_info from_table, gs_link link 
                        WHERE link.id_2 = \'#{id}\' AND link.link_type IN (\'S\')
                        AND link.id_1 = from_table.e_id',
                   :class_name => 'GsInfo'
end
>> Company.find(51).stats.all(:limit => 3)
ActiveRecord::StatementInvalid: Mysql::Error: Operand should contain 1 column(s): SELECT * FROM `gs_info` WHERE (SELECT from_table.* 
                        FROM gs_info from_table, gs_link link 
                        WHERE link.id_2 = '51' AND link.link_type IN ('S')
                        AND link.id_1 = from_table.e_id) LIMIT 3
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:219:in `log'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:323:in `execute'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:608:in `select'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/base.rb:661:in `find_by_sql'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/base.rb:1548:in `find_every'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/base.rb:615:in `find'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/base.rb:635:in `all'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:380:in `send'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:380:in `method_missing_without_paginate'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/base.rb:2143:in `with_scope'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:206:in `send'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:206:in `with_scope'
    from /Volumes/RAID/projects/12/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:376:in `method_missing_without_paginate'
    from /opt/local/lib/ruby/gems/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/finder.rb:170:in `method_missing'
    from (irb):11
    from :0

Comments and changes to this ticket

  • Santiago Pastorino

    Santiago Pastorino April 12th, 2010 @ 01:12 AM

    Please can you provide a test_case or at least upload a zip with your application.
    Thanks for helping.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:44 PM

    • State changed from “new” to “open”
    • Importance changed from “” to “Low”

    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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:44 PM

    • 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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages