:neil_middleton

Ruby on Rails, Web Application Development and bollocks extraordinaire 
« Back to blog

Adding a LIKE criteria to a Rails Conditions block

Earlier I had a interesting problem setting conditions on a query using Rails and ActiveRecord.


Consider the following code which is to be thrown at an AR find:


I needed to add another condition which is a LIKE criteria on a 'profile' attribute. How could I do this, as obviously a LIKE is usually done via an array, not a hash key?

Well, as usual, it's actually pretty simple through the use of anonymous scopes.  You can pass around these scopes as first class objects using 'scoped' (a named scoped provided to you for free) as a way to build hairy queries on the fly.


So, to solve my original problem, I simply need to do this:


Job Done.

Comments (0)

Leave a comment...