Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a LIKE criteria to a Rails Conditions block

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

conditions = []
conditions[:age] = params[:age] if params[:age].present?
conditions[:gender] = params[:gender] if params[:gender].present?

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

like image 933
Neil Middleton Avatar asked Apr 28 '10 09:04

Neil Middleton


1 Answers

You can scope your model with hash conditions, and then perform find on scope with array conditions:

YourModel.scoped(:conditions => conditions).all(:conditions => ["profile like ?", profile])
like image 92
Voyta Avatar answered Sep 20 '22 04:09

Voyta