Rails的ActiveRecord的,其中或条款 [英] Rails ActiveRecord where or clause

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

问题描述

我期待写一个ActiveRecord的查询,这就是我下面。不幸的是,你不能使用OR这样。什么是执行最好的方法是什么? category_ids 是一个整数数组。

 。凡(categories.id=> category_ids).or.where(category_relationships.category_id=> category_ids)
 

解决方案

一个办法是恢复到原始SQL ...

  YourModel.where(categories.id IN?或category_relationships.category_id吗?,category_ids,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)

解决方案

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

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