Need help with activerecord_any_of?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

129 Stars 30 Forks MIT License 60 Commits 9 Opened issues


MongoId's any_of like implementation for activerecord

Services available


Need anything else?

Contributors list


Feel free to fork

It's been a while since I haven't updated this project. I'm not considering it dead since it's still working, but if you want new features within it, don't hesitate to fork it and ping me, I'll mention your fork here.


This gem provides

on ActiveRecord.

is inspired by any_of from mongoid.

Its main purpose is to both :

  • remove the need to write a sql string when we want an
  • allows to write dynamic
    queries, which would be a pain with a string



It allows to compute an

like query that leverages AR's


User.where.any_of(first_name: 'Joe', last_name: 'Joe')
# => SELECT * FROM users WHERE first_name = 'Joe' OR last_name = 'Joe'

grouped conditions

You can separate sets of hash condition by explicitly group them as hashes :

User.where.any_of({first_name: 'John', last_name: 'Joe'}, {first_name: 'Simon', last_name: 'Joe'})
# => SELECT * FROM users WHERE ( first_name = 'John' AND last_name = 'Joe' ) OR ( first_name = 'Simon' AND last_name = 'Joe' )

it's plain #where syntax


set is the same kind you would have passed to #where :
Client.where.any_of("orders_count = '2'", ["name = ?", 'Joe'], {email: '[email protected]'})

with relations

You can as well pass

to other relations :
Client.where("orders_count = '2'").where.any_of({ email: '[email protected]' }, { email: '[email protected]' })

with associations

And with associations :

User.find(1).posts.where.any_of({published: false}, "user_id IS NULL")

dynamic OR queries

The best part is that

accepts other relations as parameter, to help compute dynamic
queries :
banned_users = User.where(banned: true)
unconfirmed_users = User.where("confirmed_at IS NULL")
inactive_users = User.where.any_of(banned_users, unconfirmed_users)


is the negative version of
. This will return all active users :
banned_users = User.where(banned: true)
unconfirmed_users = User.where("confirmed_at IS NULL")
active_users = User.where.none_of(banned_users, unconfirmed_users)


uses WhereChain, which has been introduced in rails-4. In rails-3, simply call
directly, without using
manual_removal = User.where(id: params[:users][:destroy_ids])
User.any_of(manual_removal, "email like ''", {banned: true})
@company.users.any_of(manual_removal, "email like ''", {banned: true})
User.where(offline: false).any_of( manual_removal, "email like ''", {banned: true})


In your Gemfile :

gem 'activerecord_any_of'

Activerecordanyof supports rails >= 3.2.13 and rails-4 (let me know if tests pass for rails < 3.2.13, I may edit gem dependencies).

Why not an
method instead ?

User.where( "email LIKE '" ).where( active: true ).or( offline: true )

What does this query do ?

where (email LIKE '' AND active = '1' )
OR offline = '1'
? Or
where email LIKE '' AND ( active = '1' OR
offline = '1' )
? This can quickly get messy and counter intuitive.

The MongoId solution is quite elegant. Using

, it is made clear which conditions are grouped through
and which are grouped through
  • User.where( "email LIKE '" ).where.any_of({ active: true }, { offline: true })
  • fakes = User.where( "email LIKE ''" ).where( active: true ); User.where.any_of( fakes, { offline: true })

Running test

Testing is done using TravisCI. You can use the wonderful wwtd gem to run all tests locally. By default, the task to run is

bundle exec rake spec
, and will run against
in memory. You can change the database like so:
DB=postgresql bundle exec rake spec
. Please note that you may need to change the credentials for your database in the
file. Do not commit those changes.

Pull requests

This gem is extracted from a pull request made to activerecord core, and still hope to be merged. So, any pull request here should respects usual Rails contributing rules when it makes sense (especially : coding conventions) to make integration in source pull request easy.



We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.