ruby on rails - Ransack search, how to search for each word by splitting input search parameter -
i experimenting using ransack gem
conduct search on model in rails. stands using basic setup.
controller:
def index @q = person.search(params[:q]) @people = @q.result(:distinct => true) end
view:
<%= search_form_for @q |f| %> <%= f.label :name_cont %> <%= f.text_field :name_cont %> <%= f.submit %> <% end %>
i have managed find lot of information conducting searches on multiple fields, however, haven't managed find me split :q
parameter , thereby enable me search each of (unknown quantity of) words in search string entered in form of search ??? , ??? , ??? ...
, rather searching entire string in 1 section
is able point me in right direction?
having spent chunk of time looking no results, opted custom, non-ransack option works well.
in view.html.erb
<%= form_tag siteindex_search_allproducts_path, :method => 'get' %> <b>search: </b> <%= text_field_tag :search, params[:search] %> <%= submit_tag "search", :name => nil %> <% end %>
in controller
@findproducts = allproduct.order('price asc').search(params[:search])
in model.rb
def self.search(*args) return [] if args.blank? cond_text, cond_values = [], [] args.each |str| next if str.blank? cond_text << "( %s )" % str.split.map{|w| "product_name ? "}.join(" , ") cond_values.concat(str.split.map{|w| "%#{w}%"}) end :conditions => [cond_text.join(" , "), *cond_values] end
Comments
Post a Comment