mysql - Ruby search on multiple fields -
in rails app need search via field in table, main problem must so: if have choosen select field in view a, must search condition, if both , b , b... have:
controller
@pre_oils = oil.by_brand(params[:oilbrand]).by_oiloiliness(params[:oiloiliness]).by_structure(params[:oilstructure]).by_size(params[:oilsize])
model
def self.by_oiloiliness(oiloiliness) if oiloiliness where("description ?", "%#{oiloiliness}%") else scoped end end def self.by_brand(brand) if brand where("manufacturer ?", "%#{brand}%") else scoped end end def self.by_structure(structure) if construction #where("structure ?", "%#{structure}%") where("description ?", "%#{structure}%") else scoped end end def self.by_size(size) if size where("capacity ?", "#{size}") else scoped end end
but searching very strange, 1 time working, no.... bad? , how search such fields, wich choosen in view?
(also ruby 1.9.3 rails 3.2.8)
maybe fetching find on queries , split them , delete duplicates?
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])
for kind of things there many gems available. can go through gems meta_search, searchlogic perform search.
once install meta_search
can this:
when have form
fields following:
<%= f.text_filed :capacity_eq %> <%= f.text_field :description_like %>
you params this:
params => {"description_like" => "xyz", "capacity_eq" => "21"} oil.search(params) #to results
mysql ruby-on-rails ruby
No comments:
Post a Comment