Rails ActiveRecord where or 子句 [英] Rails ActiveRecord where or clause

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

问题描述

我想写一个 ActiveRecord 查询,这就是我下面的内容.不幸的是,您不能像这样使用 OR.最好的执行方式是什么?category_ids 是一个整数数组.

I'm looking to write an ActiveRecord query and this is what I have below. Unfortunately you can't use OR like this. What's the best way to execute? category_ids is an array of integers.

.where(:"categories.id" => category_ids).or.where(:"category_relationships.category_id" => category_ids)

推荐答案

一种方法是恢复到原始 sql...

One way is to revert to raw sql...

YourModel.where("categories.id IN ? OR category_relationships.category_id IN ?", category_ids, category_ids)

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

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