Module: ActiverecordAnyOf::Chained
- Defined in:
- lib/activerecord_any_of.rb
Overview
Injected into WhereChain.
Instance Method Summary collapse
-
#any_of(*queries) ⇒ Object
Returns a new relation, which includes results matching any of the conditions passed as parameters.
-
#none_of(*queries) ⇒ Object
Returns a new relation, which includes results not matching any of the conditions passed as parameters.
Instance Method Details
#any_of(*queries) ⇒ Object
Returns a new relation, which includes results matching any of the conditions passed as parameters. You can think of it as a sql OR
implementation :
User.where.any_of(first_name: 'Joe', last_name: 'Joe')
# => SELECT * FROM users WHERE first_name = 'Joe' OR last_name = 'Joe'
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' )
Each #any_of set is the same kind you would have passed to #where :
Client.where.any_of("orders_count = '2'", ["name = ?", 'Joe'], {email: '[email protected]'})
You can as well pass #any_of to other relations :
Client.where("orders_count = '2'").where.any_of({ email: '[email protected]' }, { email: '[email protected]' })
And with associations :
User.find(1).posts.where.any_of({published: false}, "user_id IS NULL")
The best part is that #any_of accepts other relations as parameter, to help compute dynamic OR
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)
43 44 45 46 47 |
# File 'lib/activerecord_any_of.rb', line 43 def any_of(*queries) raise ArgumentError, 'Called any_of() with no arguments.' if queries.none? AlternativeBuilder.new(:positive, @scope, *queries).build end |
#none_of(*queries) ⇒ Object
Returns a new relation, which includes results not matching any of the conditions passed as parameters. It’s the negative version of #any_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)
57 58 59 60 61 |
# File 'lib/activerecord_any_of.rb', line 57 def none_of(*queries) raise ArgumentError, 'Called none_of() with no arguments.' if queries.none? AlternativeBuilder.new(:negative, @scope, *queries).build end |