CriteriaQuery and arbitrary conditions
CriteriaQuery is a Rails plugin which eases arbitrary query building.
From the README:
If you frequently face the problem of constructing complex dynamic queries, you will get some benefit out of this. Consider the following search form (taken from a real application):Name (first or last): [ ] Customer Category: [ Please Select ][^] Last call between: [ ] and [ ] Active Contacts only: [x] Address State: [ ] City: [ ] Street Addres: [ ]…
CriteriaQuery allows you to write the above as:
This is all fine and dandy, except using the current version (r41 from the repository), you cannot add arbitrary conditions, such as LENGTH(name) > 0.
ActiveRecord scopes to the rescue. I recently hit that in an application where we allow the admin to find only non anonymous estimates. Non anonymous is defined as having either a phone or an E-Mail address.
Of course, you will ask “why in the world aren’t your fields NULL to begin with if they don’t contain a value ?” Now, that is a very interesting question which I will now explore with gusto !
Leave a Reply