0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ransackで「SIMILAR TO」正規表現の検索ができるように

Last updated at Posted at 2021-01-26

PostgreSQL SIMILAR TO を使いたかった。

config/initializers/arel_override.rb
module ArelVisitorsPostgreSQLOverride
  # https://github.com/rails/rails/blob/v6.0.3.2/activerecord/lib/arel/visitors/postgresql.rb#L29
  def visit_Arel_Nodes_Regexp(o, collector)
    # op = o.case_sensitive ? " ~ " : " ~* "
    op = " SIMILAR TO "
    infix_value o, collector, op
  end
end

Arel::Visitors::PostgreSQL.prepend(ArelVisitorsPostgreSQLOverride)
config/initializers/ransack.rb
Ransack.configure do |config|
  config.add_predicate 'matches_regexp', arel_predicate: 'matches_regexp'
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?