I am trying to write a search form for my shops using ransack. Currently, I can search using the name_or_address_cont option which looks EITHER in the name OR in the address. If I had a shop Adidas in London and I type Adidas London there would be no results. Therefore I looked for a way to concatenate the two attributes and search by the new one. What I found in some old posts was the following code:
ransacker :search_name, :formatter => proc {|v| UnicodeUtils.downcase(v) } do |parent|
Arel::Nodes::NamedFunction.new('LOWER',
[Arel::Nodes::NamedFunction.new('concat_ws', [' ', parent.table[:name], parent.table[:address], parent.table[:id]])]
)
end
Which is supposed to search by a shop's name, address, and id. However, when I run I get an error in the UnicodeUtils. I tried changing it to v.downcase! but I get another error. Any idea how to handle this problem? Thank you!
There were two mistakes... The first one I found how to fix in the documentation where they have an example for full_name link
Which looks like
ransacker :full_name, formatter: proc { |v| v.mb_chars.downcase.to_s } do |parent|
Arel::Nodes::NamedFunction.new('LOWER',
[Arel::Nodes::NamedFunction.new('concat_ws',
[' ', parent.table[:first_name], parent.table[:last_name]])])
end
Also, it seems that the joining ' ' should be replaced by Arel::Nodes.build_quoted(' '). Found that in this issue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.