Rails, how do I chain scopes with an “OR” operator between them?

扶醉桌前 提交于 2019-12-11 22:15:54

问题


This is a question that spun out of "Rails, how do I chain scopes with an "and" operator between them?".

In the accepted answer, the code example generates SQL looking something like:

"where 'age' similar to '%(foo|bar)%' AND 'name' similar to '%(foo|bar)%' AND

... and so on.

How would I implement this if I want to chain the scopes with OR instead of AND?


回答1:


check the any_of gem.

Lets you do things like:

banned_users      = User.where(banned: true)
unconfirmed_users = User.where("confirmed_at IS NULL")
inactive_users    = User.where.any_of(banned_users, unconfirmed_users)


来源:https://stackoverflow.com/questions/26489531/rails-how-do-i-chain-scopes-with-an-or-operator-between-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!