I am using the Ransack Gem in Rails 3.1. Everything works as expected. However, I have a search form that will look up a person using a Soundex column that I am having an issue with.
In my form I have a "given_name_soundex_cont" field, that a user will enter the name "Joe". My controller than converts "joe" to a soundex code, and then Ransack looks for a match in the "given_name_soundex" column.
All matching records are returned and seems fine, except that the field where the user entered the word "joe" now shows the soundex value instead (of course because I changed the param).
So I figured I could just add a "given_name_cont" field and hide the "given_name_soundex_cont" field, but now Ransack wants to include "given_name_cont" in the query, which I don't want.
Does anyone know how I can include fields in a Ransack search form without Ransack actually evaluating them. That way I can specify which fields are ot be evaluated and which are not.
If it helps this is what I have in my controller:
def index
if !params[:q].blank? # nil.blank? and [].blank? are true
search_params = params[:q]
search_params[:given_name_soundex_cont] = convert_to_soundex(search_params[:given_name_soundex_cont])
@q = Person.search(search_params)
@results = @q.result.limit(50)
else
@q = Person.search(params[:q])
@results = []
end
end
private
def convert_to_soundex(text)
Text::Soundex.soundex(text.to_s)
end
And in my view:
<%= search_form_for @q do |f| %>
<label>Given Name:</label>
<%= f.text_field :given_name_soundex_cont %>
<%= f.submit %>
<% end %>
Create a ransacker that formats the parameter. I've not tried this, but I think it will work (via https://github.com/ernie/ransack/issues/36).
ransacker :given_name_soundex, :formatter => proc {|v| Text::Soundex.soundex(v)} do
parent.table[:given_name_soundex]
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With