ActiveRecord的或查询ActiveRecord

2023-09-08 15:39:54 作者:旧人阡陌

你怎么做的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%'))