ActiveRecord的或查询 [英] ActiveRecord OR query

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

问题描述

你怎么做的Rails 3 ActiveRecord的一个或查询。所有我找到刚才有实例和查询。

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

编辑:不使用SQL字符串

without using a SQL string!

推荐答案

使用阿雷尔

t = Post.arel_table

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

产生的SQL:

The resulting 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的或查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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