finder_sql for has_many relation <p>Given the following two (simplified) models:</p> <pre><code>class Customer &lt; ActiveRecord::Base # finder sql to include global users in the association has_many :users, :dependent =&gt; :destroy, :finder_sql =&gt; 'SELECT `u`.* FROM `users` `u` WHERE (`u`.`customer_id` = "#{id}" OR `u`.`customer_id` IS NULL)' validates_presence_of :name end class User &lt; ActiveRecord::Base # most users belong to a customer. # however, the customer_id may be NULL # to implement the concept of "global" users. belongs_to :customer validates_presence_of :email end </code></pre> <p>At first, this seems to work just fine:</p> <pre><code>&gt;&gt; Customer.first.users # =&gt; Array of users including global users </code></pre> <p>But when i try following i get an mysql error:</p> <pre><code>&gt;&gt; Customer.first.users.find_by_email("test@example.com") ActiveRecord::StatementInvalid: Mysql::Error: Operand should contain 1 column(s): SELECT * FROM `users` WHERE (`users`.`email` = 'test@example.com') AND ( SELECT `u`.* FROM `users` `u` WHERE (`u`.`customer_id` = "1" OR `u`.`customer_id` IS NULL) ORDER BY u.nickname ) LIMIT 1 from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:188:in `log' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:309:in `execute' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:563:in `select' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:635:in `find_by_sql' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:1490:in `find_every' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:1452:in `find_initial' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:587:in `find' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:1812:in `find_by_email' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:1800:in `send' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:1800:in `method_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:370:in `send' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:370:in `method_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2003:in `with_scope' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:202:in `send' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:202:in `with_scope' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:366:in `method_missing' </code></pre> <p>So, Rails is generating an invalid SQL Statement. The reason for that is probably my finder_sql, because the association without finder_sql works as expected.</p> <p>I've already tried to remove all optional plugins (will_paginate). The thing I'm trying to achieve is actually quite simple: Include all User Models matching the following the condition <code>(users.customer_id = 1 OR users.customer_id IS NULL)</code> in my association.</p> <p>Appreciate any help!</p>