Forum: Ruby on Rails IF- statements in controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Martyn E. (Guest)
on 2007-05-15 19:52
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.
This topic is locked and can not be replied to.