简体   繁体   English

使用OR运算符和动态参数使用Rails发出查询

[英]Issuing a query with Rails using OR operator with dynamic params

What is the best way to issue a query with an "OR" operator with dynamic params within Rails? 在Rails中使用带有动态参数的“或”运算符发出查询的最佳方法是什么? I have a simple API that checks whether the user wants to search in certain categories. 我有一个简单的API,可以检查用户是否要在某些类别中进行搜索。

categories = params[:categories].split(',')

I want to make a query that seems like this 我想做一个看起来像这样的查询

category_1 = 1 OR category_2 = 1 OR ...

I currently build a string iterating through categories and issue 我目前建立一个遍历类别和问题的字符串

@resource = Resource.where(query)

Is this the best way to make a query of this type? 这是进行此类查询的最佳方法吗?

"best" doesn't really mean much here, different methods have their pros and cons. “最好”在这里并不意味着什么,不同的方法各有利弊。 There's certainly nothing wrong with doing what you're doing (in fact I recommended essentially this method earlier today here ) 做您的工作当然没有错(实际上,我今天早些时候在这里推荐了这种方法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM