I have two models, Foo and Bar, with same attributes, and I’m ‘caching’ user “search params” into a session variable, that ends into something like this:
session[:search_params] => {"price"=>"15.0", "air_conditioner"=>"0", "fireplace"=>"0", "number_of_rooms"=>"1", "balcony"=>"0"}
I’d like to search both Foo and Bar for entries that match this query.
For now, I’m doing:
@foo = Foo.new(session[:search_params])
search_attributes = @foo.attributes
search_attributes.delete 'id'
search_attributes.delete 'created_at'
search_attributes.delete 'updated_at'
@results = Foo.where(search_attributes)
@results += Bar.where(search_attributes)
that is REALLY UGLY.
Could anyone tell me a better/the correct way?
Thanks in advance.
EDIT
Also, some params in the session[:search_params] that are “0”, are actually ‘false’ on the database (booleans) (filled by checkboxes), so I have to do some “conversion” into this fields so that the query is done correctly, i.e., getting for example air_conditioner => 'false' instead of air_conditioner => '0'.
I wouldn’t follow that path if I were you. I would go with a solution like https://github.com/ernie/meta_search