New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle special characters #332
Comments
Hi @grillorafael there are different ways you could handle special characters. One would be to remove them from the search query and the search field in the database. For this I've been using In
in your view partial:
and search against a dedicated, indexed search column in your database with accents removed, using for example a
In addition to removing accents, you could also remove spaces and punctuation depending on your needs:
Or use a specific character to separate the searched fields. Make sure you do the same thing in both your predicate method in |
Thanks, it worked for me! |
This is great. |
Since I lost a couple of hours with this issue today, I'm going to share what I had to do to make the proposed solution work. I'm using PostgreSQL and I also wanted to override the # on initializers/ransack.rb
Ransack.configure do |config|
config.add_predicate 'cont', # Name your predicate
arel_predicate: 'matches',
formatter: proc { |s| ActiveSupport::Inflector.transliterate("%#{s}%") }, # Note the %%
validator: proc { |s| s.present? },
compounds: true,
type: :string
end As for the database part, I used another solution, from #349 ransacker :name, type: :string do
Arel.sql("unaccent(\"name\")")
end And in some view
Note that you have to add the unnacent postgres extension for this to work class AddUnnacentExtension < ActiveRecord::Migration[5.0]
def up
execute 'CREATE EXTENSION IF NOT EXISTS unaccent;'
end
def down
execute 'DROP EXTENSION IF EXISTS unaccent CASCADE;'
end
end |
Since I lost a couple of hours too I would like to share my solution. As I wanted to add the capability to use What I ended doing (and ended working 😄 ) was: Add the This gem adds a lot of interesting new nodes and predicates for gem 'arel_extensions' Create a new ransacker predicate that uses this Arel predication in Ransack.configure do |config|
config.add_predicate(
# produces queries in the form of `WHERE (unaccent (column)) ILIKE unaccent (%foo%bar%)`
'ucont',
arel_predicate: 'ai_imatches', # <- thanks arel_extensions !
formatter: proc { |s| ActiveSupport::Inflector.transliterate("%#{s.tr(' ', '%')}%") },
validator: proc { |s| s.present? },
compounds: true,
type: :string
)
end (the replacement of spaces by From now on you can use the You can also create a ransacker :full_name do |parent|
Arel::Nodes::InfixOperation.new(
'||',
parent.table[:first_name],
Arel::Nodes::InfixOperation.new(
'||',
parent.table[:last_name],
parent.table[:middle_name]
)
)
end Then in some view you can do
🎉 |
Hi, guys!
I'm having a problem here.
I have a record with the following name: 'Pão de Ló'
When I search for 'p', I get my result fine but when I search for 'pa' I get no results.
Is there any way to match these characters?
The text was updated successfully, but these errors were encountered: