/ar_or_where_test

A proof-of-concept for merging scopes with OR clause

Primary LanguageRuby

Active Record OR clause - proof-of-concept

NOTE: This is a proof-of-concept project.

If you are serious about using “OR” clauses with ActiveRecord, take a look at Squeel instead.

How it works

Let’s assume you have the following scopes:

scope :johnes, where(:first_name => "John")
scope :smiths, where(:last_name => "Smith")

Once you require “lib/ar_or_where” file, you can combine the scopes like this:

User.johnes.or(User.smiths)
=> SELECT "users".* FROM "users"
   WHERE (("users"."first_name" = 'John'
   OR "users"."last_name" = 'Smith'))

Please find the example in spec/models/user_spec.rb