LoginSignup
1
0

More than 3 years have passed since last update.

Railsでレコードを正規表現検索

Last updated at Posted at 2020-09-05

PostgreSQLでしか使えませんが、ActiveRecordで正規表現検索をする必要があったので、スコープを自作しました。

scope :with_regexp, ->(column, pattern) {
    pattern = pattern.source if pattern.class == Regexp
    where("#{table_name}.#{column} ~ ?", pattern) if columns_hash[column.to_s].type == :string
}
1
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
1
0