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