找到列可以有多个值的地方,rails [英] find where column could have multiple values, rails

查看:38
本文介绍了找到列可以有多个值的地方,rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试搜索一列可能等于一个值而另一列可能有多个值的位置...

I'm trying to search where one column could equal one value and the other could have multiple values...

这是有效的,每个值一个......

This works, with one value for each...

<%= Post.where("category LIKE ? OR commentary LIKE?", 20, "%mom%").count %>

这不起作用...

<%= Post.where("category LIKE ? OR commentary LIKE?", 20, [{"%mom%", "%mother%"}]).count %>

在 Rails 中解决这个问题的最佳方法是什么?

How would be the best way to solve this in rails?

谢谢.

推荐答案

你要做的只是使用 IN

您可以按照以下操作;

<%= Post.where("category LIKE ? OR commentary LIKE? OR commentary LIKE?", 20, "%mom%", "%mother%"]).count %>

这将生成以下sql查询;

This will generate the following sql query;

SELECT Count(*) FROM `posts` WHERE (category like "20" OR commentary like "%mom%" OR commentary like "%mother%" )

这篇关于找到列可以有多个值的地方,rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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