在范围查询ActiveRecord的OR子句

编程入门 行业动态 更新时间:2024-10-23 09:37:59
本文介绍了在范围查询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子句

本文发布于:2023-10-23 22:46:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子句   ActiveRecord

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!