问题
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