我有两个模型,Foo和Bar,具有相同的属性,并且将'用户'搜索参数'缓存到会话变量中,像这样:Rails 3:将会话[:params]转换为正确的activerecord查询
session[:search_params] => {"price"=>"15.0", "air_conditioner"=>"0", "fireplace"=>"0", "number_of_rooms"=>"1", "balcony"=>"0"}
我想搜索这两个Foo
和Bar
相匹配此查询的条目。
现在,我在做:
@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)
这是十分可怕的。
任何人都可以告诉我一个更好/正确的方法吗?
在此先感谢。
编辑
而且,有些PARAMS在session[:search_params]
是“0”,是对数据库实际上“假”(布尔)(由复选框填写),所以我不得不做一些“军转民”进入这个领域,以便query
正确完成,即获得例如air_conditioner => 'false'
而不是air_conditioner => '0'
。
谢谢,我会仔细看看。 –