My initial thought was more around having a new query method (where.or) to solve the specific problem I explained on my first post. However, I've been thinking about it and I now believe that it's worth trying to tackle the whole "OR problem".
The problem
Creating where clauses more complicated than simple chained AND conditions could be challenging with ActiveRecord. If you need to add an OR condition to a query, you need to use the or method which expects a Relation object as parameter. That means that you have to specify the model you are using and call where again. That's not great. This problem gets multiplied if you have a query with multiple OR conditions as you have to chain them by using or.
My wish list
The missing functionality exposed on the previous point gets captured in the next list of 2 wishes:
- I want to be able to choose the keyword (AND or OR) to be used to chain a new condition (or series of conditions) with any previous ones.
- I want to be able to choose the keyword (AND or OR) to be used to join a list of conditions.
My proposal
I propose the following new methods and variations of the where method to solve the problem:
- where.and() to chain with AND.
- where.or() to chain with OR.
- where(:and, hash_conditions) to join hash_conditions with AND.
- where(:or, hash_conditions) to join hash_conditions with OR.
Obviously, this proposal should't break any of the existing behaviour and the where method should still work in the way it does now. Specifically, where.and is an alias of where, and where(:and, hash_conditions) should work exactly the same as where(hash_conditions).
What are your thoughts on this?
Thanks!