Ruby on rails Ransack gem-不区分大小写的搜索

Ruby on rails Ransack gem-不区分大小写的搜索,ruby-on-rails,case-insensitive,ransack,Ruby On Rails,Case Insensitive,Ransack,使用搜救宝石,我想这样做 但这种方法还不起作用 所以我试着去想,有没有其他方法可以做到这一点 我得到了一些关于如何做的信息 //in model ransacker :ig_case, formatter: proc { |v| v.mb_chars.upcase.to_s } do |parent| Arel::Nodes::NamedFunction.new('UPPER',[parent.table[:firstname]]) end //in config/ranr

使用搜救宝石,我想这样做

但这种方法还不起作用

所以我试着去想,有没有其他方法可以做到这一点 我得到了一些关于如何做的信息

//in model

  ransacker :ig_case, formatter: proc { |v| v.mb_chars.upcase.to_s } do |parent|
    Arel::Nodes::NamedFunction.new('UPPER',[parent.table[:firstname]])
  end

//in config/ranrack.rb

Ransack.configure do |config|
  config.add_predicate 'ig_case', # Name your predicate
    arel_predicate: 'matches',
    formatter: proc { |v| "%#{v.to_s.gsub(/([\\|\%|.])/, '\\\\\\1').mb_chars.upcase}%"},
    validator: proc { |v| v.present? },
    compounds: true,
    type: :string
end

   // use way
User.search({ firstname_or_lastname_ig_case: "ABC"}).result.to_sql

=> "SELECT `Users`.* FROM `Users` WHERE ((UPPER(`users`.`firstname`) LIKE '%ABC%' OR (`users`.`lastname`) LIKE '%ABC%'))"
几个小时后,我发现当我以模型方式使用时,每次只能得到一个大写字段

如果我以配置的方式选择,我可以对所有字段进行升级,但我不能得到这样的sql 'UPPER(“用户”。'first_name')'


有什么解决办法吗?我真的非常感谢。

您需要通过执行以下操作来覆盖适配器中的Arel:

    module Arel

      module Nodes
        %w{
          IDoesNotMatch
          IMatches
        }.each do |name|
          const_set name, Class.new(Binary)
        end
      end

      module Predications
        def i_matches other
          Nodes::IMatches.new self, other
        end

        def i_does_not_match other
          Nodes::IDoesNotMatch.new self, other
        end
      end

      module Visitors

        class ToSql < Arel::Visitors::Visitor
          def visit_Arel_Nodes_IDoesNotMatch o
            "UPPER(#{visit o.left}) NOT LIKE UPPER(#{visit o.right})"
          end

          def visit_Arel_Nodes_IMatches o
            "UPPER(#{visit o.left}) LIKE UPPER(#{visit o.right})"
          end
        end

        class Dot < Arel::Visitors::Visitor
          alias :visit_Arel_Nodes_IMatches            :binary
          alias :visit_Arel_Nodes_IDoesNotMatch       :binary
        end

        class DepthFirst < Visitor

          unless method_defined?(:visit_Arel_Nodes_InfixOperation)
            alias :visit_Arel_Nodes_InfixOperation :binary
            alias :visit_Arel_Nodes_IMatches            :binary
            alias :visit_Arel_Nodes_IDoesNotMatch       :binary
          end

        end

      end
    end
模块Arel
模块节点
%w{
我不匹配
伊玛契
}.每个人都有自己的名字|
const_集合名称,Class.new(二进制)
结束
结束
模块谓词
def i_匹配其他
节点::IMatches.new self,其他
结束
定义i_与其他不匹配
节点::IDoesNotMatch.new self,其他
结束
结束
模块访问者
类ToSql
除此之外,还需要提供谓词的方法

这是我的宝石叉子,可以解决您的问题:

我有一个PR被关闭了,因为它可能破坏了其他适配器。到目前为止,它在我的应用程序中运行得非常好:


另外请注意,如果您有任何索引列,它们将被忽略,因为您使用的是UPPER。

您需要通过执行以下操作覆盖适配器中的Arel:

    module Arel

      module Nodes
        %w{
          IDoesNotMatch
          IMatches
        }.each do |name|
          const_set name, Class.new(Binary)
        end
      end

      module Predications
        def i_matches other
          Nodes::IMatches.new self, other
        end

        def i_does_not_match other
          Nodes::IDoesNotMatch.new self, other
        end
      end

      module Visitors

        class ToSql < Arel::Visitors::Visitor
          def visit_Arel_Nodes_IDoesNotMatch o
            "UPPER(#{visit o.left}) NOT LIKE UPPER(#{visit o.right})"
          end

          def visit_Arel_Nodes_IMatches o
            "UPPER(#{visit o.left}) LIKE UPPER(#{visit o.right})"
          end
        end

        class Dot < Arel::Visitors::Visitor
          alias :visit_Arel_Nodes_IMatches            :binary
          alias :visit_Arel_Nodes_IDoesNotMatch       :binary
        end

        class DepthFirst < Visitor

          unless method_defined?(:visit_Arel_Nodes_InfixOperation)
            alias :visit_Arel_Nodes_InfixOperation :binary
            alias :visit_Arel_Nodes_IMatches            :binary
            alias :visit_Arel_Nodes_IDoesNotMatch       :binary
          end

        end

      end
    end
模块Arel
模块节点
%w{
我不匹配
伊玛契
}.每个人都有自己的名字|
const_集合名称,Class.new(二进制)
结束
结束
模块谓词
def i_匹配其他
节点::IMatches.new self,其他
结束
定义i_与其他不匹配
节点::IDoesNotMatch.new self,其他
结束
结束
模块访问者
类ToSql
除此之外,还需要提供谓词的方法

这是我的宝石叉子,可以解决您的问题:

我有一个PR被关闭了,因为它可能破坏了其他适配器。到目前为止,它在我的应用程序中运行得非常好:


另外请注意,如果您有任何索引列,它们将被忽略,因为您使用的是UPPER。

Awesome!很乐意帮忙。介意把它作为答案吗?太棒了!很乐意帮忙。介意把它作为答案吗?