# Using like in multiple conditions

**URL:** <https://www.ruby-forum.com/t/using-like-in-multiple-conditions/76840>\
**Category:** Rails\
**Created:** [November 21, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/using-like-in-multiple-conditions/76840 "2006-11-21T17:59:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 21, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/using-like-in-multiple-conditions/76840/1 "2006-11-21T17:59:24Z")

</div>

From the beta version of the Agile book V2, I can understand the  
following:

User.find(:all, :conditions =\> [“name like ?” , params[:name]+"%" ])

yet\_more = Order.find(:all,  
:conditions =\> [“name = :name and pay\_type = :pay\_type”,  
params[:order]])

What I want to do is to use the like operator for the name in this  
second example, but I am getting cofused in how the condition should be  
coded?

Thanks

Andrew

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 21, 2006, 8:10pm UTC](https://www.ruby-forum.com/t/using-like-in-multiple-conditions/76840/2 "2006-11-21T20:10:41Z")

</div>

You can modify the value of params[:order] in your controller/action.

def results  
params[:order][:name] += ‘%’  
yet\_more = Order.find(:all, :conditions =\> [“name LIKE :name AND  
pay\_type = :pay\_type”, params[:order]])  
end

This will add the % operator to the end of the name string being passed  
from the submitted form.

-Jared

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 21, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/using-like-in-multiple-conditions/76840/3 "2006-11-21T21:16:51Z")

</div>

Jared, that works. Thanks a lot.

I had to modify it slightly as the name parameter could be nil so I  
used:

params[:order][:name] = params[:order][:name].to\_s + ‘%’

That works for me, I’m not sure if there is a more elegant or better way  
of dealing with name being nil.

Andrew

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 21, 2006, 11:25pm UTC](https://www.ruby-forum.com/t/using-like-in-multiple-conditions/76840/4 "2006-11-21T23:25:16Z")

</div>

Consider wrapping this up in a custom finder on Order… that way you  
can

1. put conditions in the model and do evaluations on the fields  
there…  
keeping controller nice and clean.
2. unit test this a lot easier

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [November 21, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/using-like-in-multiple-conditions/76840/5 "2006-11-21T22:17:04Z")

</div>

If the params[:order][:name] parameter is left blank on your search  
form, you could do something like this in your controller:

unless params[:order][:name].blank?  
params[:order][:name] += ‘%’  
else  
params[:order][:name] = ‘%’  
end  
yet\_more = Order.find(:all, :conditions =\> [“name LIKE :name AND  
pay\_type = :pay\_type”, params[:order]])

That way, if the field is left blank on the search form, the  
Order.find() method will include all names, and just match on the  
pay\_type. I don’t know if I’d call it more elegant, but it’s a bit  
more explicit in what the code is doing.

-Jared
