简体   繁体   中英

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? I have a simple API that checks whether the user wants to search in certain categories.

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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