ActiveRecord OR 查询 [英] ActiveRecord OR query

查看:34
本文介绍了ActiveRecord OR 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Rails 3 ActiveRecord 中进行 OR 查询.我找到的所有示例都只有 AND 查询.

How do you do an OR query in Rails 3 ActiveRecord. All the examples I find just have AND queries.

OR 方法自 Rails 5 起可用.请参阅 ActiveRecord::QueryMethods

OR method is available since Rails 5. See ActiveRecord::QueryMethods

推荐答案

使用 ARel

t = Post.arel_table

results = Post.where(
  t[:author].eq("Someone").
  or(t[:title].matches("%something%"))
)

结果 SQL:

ree-1.8.7-2010.02 > puts Post.where(t[:author].eq("Someone").or(t[:title].matches("%something%"))).to_sql
SELECT     "posts".* FROM       "posts"  WHERE     (("posts"."author" = 'Someone' OR "posts"."title" LIKE '%something%'))

这篇关于ActiveRecord OR 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆