Ransack search for integer and string values from single search field

1.5k Views Asked by At

I am using Rails 4 with Ransack.

I cant make to work searching for integer and string values.

So far I tried this code.

In view:

<%= search_form_for @search do |f| %>
  <div class="field">
    <%= f.label :name_cont, "Nepieciešama meklēšāna pēc ID, vārda un telefona nr" %>         
    <%= f.text_field :name_or_phone_number_cont_or_id_eq, :placeholder => 'Meklēt pēc ID,vārda vai tel.nr.' %>
  </div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>

In controller :

 def index

    @search = Advertisement.search(params[:q])
    @advertisements = @search.result

   respond_with(@advertisements)
  end

This gives me error:

undefined method `name_or_phone_number_cont_or_id_eq' for

Is there any workaround to this problem? Or I need to convert id to string for search purposes ?

Thanks in advance.

1

There are 1 best solutions below

1
On

you need to add the code to your model, for you to be able to search for string and integer.

ransacker :id do
    Arel.sql("to_char(\"#{table_name}\".\"folio\", '99999999')")
  end