# Find Conditions

**URL:** <https://www.ruby-forum.com/t/find-conditions/132862>\
**Category:** Rails\
**Created:** [March 25, 2008, 1:04pm UTC](https://www.ruby-forum.com/t/find-conditions/132862 "2008-03-25T13:04:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shawn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Shawn\_B](https://www.ruby-forum.com/u/Shawn_B)\
**Post date:** [March 25, 2008, 1:04pm UTC](https://www.ruby-forum.com/t/find-conditions/132862/1 "2008-03-25T13:04:41Z")

</div>

Is it possible to us an if statement in a find condition statement

Controller:  
def index  
@companies = Company.search(params[:province])  
respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @companies }  
end  
end

Model:  
def self.search(search)  
if search  
find(:all, :conditions =\> [‘province LIKE ?’, “%#{search}%”])  
else  
find(:all)  
end  
end

I want to be able to check the URL to see if there are any other  
parameters being passed and if so use them in the condition statemetn

find(:all, :conditions =\> [‘province LIKE ? and wood\_based\_panels =  
1’, “%#{search}%”])

I would like to check to see if the wood\_based\_panels variable was  
passed in the URL if so I would like to add it to the statement if not  
I would like to remove it and only have the following.

find(:all, :conditions =\> ['province LIKE ?, “%#{search}%”])

---

<div class="post-metadata">

**Author:** ![Shawn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Shawn\_B](https://www.ruby-forum.com/u/Shawn_B)\
**Post date:** [March 25, 2008, 1:16pm UTC](https://www.ruby-forum.com/t/find-conditions/132862/2 "2008-03-25T13:16:45Z")

</div>

Shawn B wrote:

> Model:  
> def self.search(search)  
> if search  
> find(:all, :conditions =\> [‘province LIKE ?’, “%#{search}%”])  
> else  
> find(:all)  
> end  
> end

Firstly, always remember these are statements with variables. You can  
pull the  
above apart like this:

def self.search(search)  
condition = [‘province LIKE ?’, “%#{search}%”] if search  
find(:all, :conditions =\> condition)  
end

> I want to be able to check the URL to see if there are any other  
> parameters being passed and if so use them in the condition statemetn

Then don’t pass in params[:province]. Pass in params, and use it  
directly in a  
named argument replacement thing, like this (someone check my math!):

condition = ‘1 = 1’  
params.each do |k,v|  
condition \<\< " AND #{k} LIKE ‘%:#{k}%’"  
end  
return find(:all, :conditions =\> [condition, params])

That way, whatever new params you think of, the method will  
automatically accept.

(This logic assumes LIKE ‘%%’ matches anything. Write unit tests to  
check that.  
Otherwise, put unless v.blank? on the end of the line with the \<\<.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![Shawn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Shawn\_B](https://www.ruby-forum.com/u/Shawn_B)\
**Post date:** [March 25, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/find-conditions/132862/3 "2008-03-25T13:42:29Z")

</div>

Thanks for the quick response. I am very new to Ruby and wondering if  
you can help me a little bit more on this topic.

I tried your suggestions as follows

New Controller:  
def index  
@companies = Company.search(params[])  
respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @companies }  
end  
end

New Method:  
def self.search(search)  
condition = ‘1 = 1’  
params.each do |k,v|  
condition \<\< " AND #{k} = ‘:#{k}’"  
end  
return find(:all, :conditions =\> [condition, params])

```
 condition = ['province LIKE ?', "%#{search}%"] if search
 find(:all, :conditions => condition)

```

end

I am getting an error now as follows:  
wrong number of arguments(0 for 1)
