views:

810

answers:

2

Given the following two (simplified) models:

class Customer < ActiveRecord::Base
  # finder sql to include global users in the association
  has_many :users, :dependent => :destroy,
  :finder_sql => 'SELECT `u`.* FROM `users` `u`
  WHERE (`u`.`customer_id` = "#{id}" OR `u`.`customer_id` IS NULL)'

  validates_presence_of :name
end

class User < 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

At first, this seems to work just fine:

>> Customer.first.users # => Array of users including global users

But when i try following i get an mysql error:

>> Customer.first.users.find_by_email("[email protected]")
ActiveRecord::StatementInvalid: Mysql::Error: Operand should contain 1 column(s): 
SELECT * FROM `users` 
WHERE (`users`.`email` = '[email protected]') 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'

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.

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 (users.customer_id = 1 OR users.customer_id IS NULL) in my association.

Appreciate any help!

+1  A: 

Either of these should work

User.find(:all, :conditions => ['customer_id = ? OR customer_id IS NULL', Customer.first[:id]])

or

Customer.first.users.select{|user| user.email == "[email protected]"}
DanSingerman
+2  A: 

You might find it easier if you actually have a special "global" customer so you don't need to deal with the awkwardness of the NULL.

This is an example of using the Null Object pattern.

floehopper