简体   繁体   English

Rails,使用Ransack gem搜索,将字符串小写

[英]Rails, search with Ransack gem, downcase a string

I am trying to write a search form for my shops using ransack. 我正在尝试使用ransack为我的商店编写搜索表单。 Currently, I can search using the name_or_address_cont option which looks EITHER in the name OR in the address. 目前,我可以使用name_or_address_cont选项进行搜索,该选项的名称或地址看起来都为EITHER。 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. 应该根据商店的名称,地址和ID进行搜索。 However, when I run I get an error in the UnicodeUtils. 但是,当我运行时,我在UnicodeUtils中遇到错误。 I tried changing it to v.downcase! 我尝试将其更改为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 有两个错误...第一个错误是我在文档中找到了如何修复full_name 链接的示例的方法。

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(' '). 另外,似乎应将连接的''替换为Arel :: Nodes.build_quoted('')。 Found that in this issue 发现这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM