# IF- statements in controller

**URL:** <https://www.ruby-forum.com/t/if-statements-in-controller/104228>\
**Category:** Rails\
**Created:** [May 15, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/if-statements-in-controller/104228 "2007-05-15T17:52:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [May 15, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/if-statements-in-controller/104228/1 "2007-05-15T17:52:01Z")

</div>

I would appreciate a little help with use of :condition

I hav ethe following code in a controller file:-

```
  def listbyparam
    if params[:department_id] != ''
           conditions = ["department_id = ?",

```

params[:department\_id]]  
end  
if params[:status] != ‘’  
conditions = [“Status = ?”, params[:status]]  
end  
@risksfound = Risk.find(:all, :conditions =\> conditions)  
end

User either clicks a link that passes a department parameter through or  
a link that passes a status parameter. I then load the condition field  
and retrieve records based on it.

What actually happens is that with the above code; if I send a status  
through it works fine but if I send a department through it finds  
nothing. However if I reverse the two if statments - putting the status  
one first and then department, the department search will then work but  
the status returns nothing.

It seems that whichever is the 2nd IF statement will blank the  
‘condition’ field even if its parameter is passed through as blank and  
the condition field has been loaded with a value by the 1st IF  
statement.

Can someone please suggest a better syntax for me to load the condition  
field depending on which parameter I click to pass through to the  
controller.

Any help - much appreciated.
