Railsアプリでは、テーブル内のフィールドを介して検索する必要がありますが、主な問題は、そうしなければならないことです。ビューAでフィールドを選択することを選択した場合、A条件のみで検索する必要があります。AとBの両方の場合は、 AとB...今私は持っています:
コントローラ
@pre_oils = Oil.by_brand(params[:oilbrand]).by_oiloiliness(params[:oiloiliness]).by_structure(params[:oilstructure]).by_size(params[:oilsize])
モデル
def self.by_oiloiliness(oiloiliness)
if oiloiliness
where("description LIKE ?", "%#{oiloiliness}%")
else
scoped
end
end
def self.by_brand(brand)
if brand
where("manufacturer LIKE ?", "%#{brand}%")
else
scoped
end
end
def self.by_structure(structure)
if structure
#where("structure LIKE ?", "%#{structure}%")
where("description LIKE ?", "%#{structure}%")
else
scoped
end
end
def self.by_size(size)
if size
where("capacity LIKE ?", "#{size}")
else
scoped
end
end
しかし、それは非常に奇妙な検索を行っています。あるときは機能していて、別のときは機能していません。そして、そのようなフィールドだけで検索する方法は、ビューで選択されていますか?
(ルビー1.9.3レール3.2.8も)
たぶん、すべてのクエリで検索を使用してフェッチし、それらを分割して重複を削除するものはありますか?
upd
oil = Oil.brand_like(params[:oilbrand])
oil = Oil.description_like(params[:oiloiliness])
oil = Oil.description_like(params[:oilstructure])
oil = Oil.capacity_eq(params[:oilsize])
upd2
@pre_oils = Oil.search(:manufacturer_like => params [:oilbrand]、:description_like => params [:oiloiliness]、:description_like => params [:oilstructure]、:capacity_eq => params [:oilsize])