给出以下两个(简化)模型:
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
起初,这似乎工作正常:
>> Customer.first.users # => Array of users including global users
但是当我尝试跟随时,我得到一个mysql错误:
>> 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'
因此,Rails正在生成无效的SQL语句。原因可能是我的finder_sql,因为没有finder_sql的关联按预期工作。
我已经尝试删除所有可选插件(will_paginate)。我想要实现的目标实际上非常简单:在我的关联中包含与以下条件(users.customer_id = 1 OR users.customer_id IS NULL)
匹配的所有用户模型。
感谢任何帮助!
答案 0 :(得分:3)
如果你真的有一个特殊的“全球”客户,你可能会发现它更容易,所以你不需要处理NULL的尴尬。
这是使用Null Object pattern。
的示例答案 1 :(得分:1)
其中任何一个都应该起作用
User.find(:all, :conditions => ['customer_id = ? OR customer_id IS NULL', Customer.first[:id]])
或
Customer.first.users.select{|user| user.email == "test@example.com"}