Rails 5:ActiveRecord OR 查询 [英] Rails 5: ActiveRecord OR query

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

问题描述

您如何在 Rails 5 ActiveRecord 中执行 or 查询?另外,是否可以在 ActiveRecord 查询中将 orwhere 链接起来?

How do you do an or query in Rails 5 ActiveRecord? Also, is it possible to chain or with where in ActiveRecord queries?

推荐答案

or 子句与 ActiveRecord 中的 where 子句链接起来的能力查询将在 Rails 5 中可用.请参阅相关讨论和拉取请求.

The ability to chain or clause along with where clause in ActiveRecord query will be available in Rails 5. See the related discussion and the pull request.

因此,您将能够在 Rails 5 中执行以下操作:

So, you will be able to do the following things in Rails 5:

要获得 id 1 或 2 的 post:

To get a post with id 1 or 2:

Post.where('id = 1').or(Post.where('id = 2'))

其他一些例子:

(A && B) ||C:

    Post.where(a).where(b).or(Post.where(c))

(A || B) &&C:

    Post.where(a).or(Post.where(b)).where(c)

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

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