本文介绍了在范围查询ActiveRecord的OR子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
状态=%W(病健康饥饿)
查询= User.scoped(:加入=>'左外连接宠物上pets.user_id = users.id',:条件=> {'pets.id'=>零,users.job_status'=>为static})
由于code以上,才有可能or子句添加到:条件来这样说:
其中(pets.id IS NULL和users.status IN('有病','健康','饿'))OR(users.gender ='男')解决方案
您可以做使用 MetaWhere 宝石
状态=%W(病健康饥饿) 查询= User.include(宠物)。凡( (pets.id'=>零,users.job_status'=>的状态)| (users.gender'=>男性) )
在 | 符号用于或状态。
PS :在包括指令使用 LEFT OUTER JOIN 所以没有必要手$ C C $加入。
statuses = %w(sick healthy hungry) query = User.scoped(:joins => 'left outer join pets on pets.user_id = users.id', :conditions => { 'pets.id' => nil, 'users.job_status' => stati })Given the code above, is it possible to add an OR clause to the :conditions to say something like
where (pets.id is NULL AND users.status IN ('sick', 'healthy', 'hungry')) OR (users.gender = 'male')解决方案
You can do the following using the MetaWhere gem
statuses = %w(sick healthy hungry) query = User.include(:pets).where( ('pets.id' => nil, 'users.job_status' => statuses) | ('users.gender' => 'male') )The | symbol is used for OR condition.
PS : The include directive uses LEFT OUTER JOIN so there is no need to hand code the JOIN.
更多推荐
在范围查询ActiveRecord的OR子句
发布评论