How to turn on REGEXP in SQLite3 and Rails 3.1?

后端 未结 6 1925
谎友^
谎友^ 2020-12-11 16:28

I have the following statement in Rails 3 using an SQLite3 database:

word = \'Hello\'
word_entry = Word.where(\"name REGEXP :word\", {:word => \"[[:<:]         


        
6条回答
  •  悲&欢浪女
    2020-12-11 17:11

    From source of sqlite3_ar_regexp project, I extract this:

        db = ActiveRecord::Base.connection.raw_connection
        db.create_function('regexp', 2) do |func, pattern, expression|
          func.result = expression.to_s.match(
              Regexp.new(pattern.to_s, Regexp::IGNORECASE)) ? 1 : 0
        end
    

    Improved upon a previous answer with ActiveRecord::Base.connection.raw_connection so that db name isn't needed

提交回复
热议问题