Find Conditions

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}%"])

Shawn B wrote:

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

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 <<.

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)