I have been working on a housing listing site and just started to implement the advanced search feature. I found that I had not handled all the edge cases in my tests. Have no fear its up and working and of course I have some code to show you.
Here it is:
def self.search(params = {})
composed_cond = EZ::Where::Condition.new
cond_number_of_bedrooms = EZ::Where::Condition.new :properties do
number_of_bedrooms == params[:search][:number_of_bedrooms]
end
cond_min_price = EZ::Where::Condition.new :properties do
if !params[:search][:min_price].blank?
price >= params[:search][:min_price]
end
end
cond_max_price = EZ::Where::Condition.new :properties do
if !params[:search][:max_price].blank?
price <= params[:search][:max_price]
end
end
composed_cond << cond_number_of_bedrooms
composed_cond << cond_min_price
composed_cond << cond_max_price
@properties = Property.find(:all,
:include =>[:amenities, :utilities],
:conditions => composed_cond.to_sql)
end
-CH