Skip to content

About

MongoId's any_of like implementation for activerecord

Resources

Stars

0 stars

Watchers

1 watching

Forks

 
 

Repository files navigation

ActiverecordAnyOf

Hey, want to take over?

These days, I write less and less ruby and more and more golang. I will continue to maintain this project, because there are people using it (don't worry, I won't let you down!).

That being said I would love if someone could take over. Please let me know in that issue if you're interested.

Introduction

This gem provides #any_of and #none_of on ActiveRecord.

#any_of 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 OR
  • allows to write dynamic OR queries, which would be a pain with a string

Usage

#any_of

It allows to compute an OR like query that leverages AR's #where syntax:

basics

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

Each #any_of set is the same kind you would have passed to #where :