Search code examples
ruby-on-railsrubyormchainscopes

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


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?


Solution

  • 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)