找到列可以包含多个值的位置,rails

时间:2021-11-13 07:58:35

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 %>

This does not work...

这并不工作……

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

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

如何在rails中解决这个问题?

Thank you.

谢谢你!

2 个解决方案

#1


2  

What you are trying to do is only work with IN

你要做的只是和IN一起工作

You can do as follow;

你可以做以下的事;

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

This will generate the following sql query;

这将生成以下sql查询;

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

#2


2  

You could add it as a scope to move the repetition to your Post class:

你可以把它作为一个范围,把重复的内容移到你的后班:

class Post < ActiveRecord::Base
  scope :categories_like, ->(to_match) { 
    where(
      [to_match.map("category like ?").join(" OR ")] + 
      (to_match.map { |m| "%#{m}%" })}

The scope adds as many category like ? clauses as there are categories to match and then adds each thing you want to match with % prefixed and suffixed. More details on adding scopes like there can be found in the rails guide. (I haven't tested the code as I'm not on my dev machine so it might need a little nudge in the right direction).

范围增加了许多类别,如?子句中有匹配的类别,然后将你想要匹配的每一项添加到%前缀和后缀中。更多关于添加作用域的详细信息可以在rails指南中找到。(我没有测试代码,因为我没有在我的开发机器上,所以它可能需要在正确的方向上稍微推动一下)。

You can then use it like:

你可以这样使用:

Post.categories_like(['mom', 'mother', 'madre']).count

As you've pointed out elsewhere it isn't the most efficient code in the whole world but then what you're asking it to isn't particularly efficient in SQL terms either.

正如您在其他地方指出的,它并不是世界上效率最高的代码,但是您所要求的代码在SQL术语中也不是特别高效。

#1


2  

What you are trying to do is only work with IN

你要做的只是和IN一起工作

You can do as follow;

你可以做以下的事;

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

This will generate the following sql query;

这将生成以下sql查询;

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

#2


2  

You could add it as a scope to move the repetition to your Post class:

你可以把它作为一个范围,把重复的内容移到你的后班:

class Post < ActiveRecord::Base
  scope :categories_like, ->(to_match) { 
    where(
      [to_match.map("category like ?").join(" OR ")] + 
      (to_match.map { |m| "%#{m}%" })}

The scope adds as many category like ? clauses as there are categories to match and then adds each thing you want to match with % prefixed and suffixed. More details on adding scopes like there can be found in the rails guide. (I haven't tested the code as I'm not on my dev machine so it might need a little nudge in the right direction).

范围增加了许多类别,如?子句中有匹配的类别,然后将你想要匹配的每一项添加到%前缀和后缀中。更多关于添加作用域的详细信息可以在rails指南中找到。(我没有测试代码,因为我没有在我的开发机器上,所以它可能需要在正确的方向上稍微推动一下)。

You can then use it like:

你可以这样使用:

Post.categories_like(['mom', 'mother', 'madre']).count

As you've pointed out elsewhere it isn't the most efficient code in the whole world but then what you're asking it to isn't particularly efficient in SQL terms either.

正如您在其他地方指出的,它并不是世界上效率最高的代码,但是您所要求的代码在SQL术语中也不是特别高效。