Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQLite3::SQLException:没有这样的函数:REGEXP rails rspec_Regex_Ruby On Rails 4_Rspec_Sqlite_Ruby 2.1 - Fatal编程技术网

SQLite3::SQLException:没有这样的函数:REGEXP rails rspec

SQLite3::SQLException:没有这样的函数:REGEXP rails rspec,regex,ruby-on-rails-4,rspec,sqlite,ruby-2.1,Regex,Ruby On Rails 4,Rspec,Sqlite,Ruby 2.1,在我的项目中,我有不同的时间片。这些时间片有“00-04”、“04-08”、“08-12”、“12-16”、“16-20”和“20-24”等名称。我想得到所有具有上述名称的时间片对象。因此,我编写了以下程序: time_slice_names = ["00-04", "04-08", "08-12", "12-16", "16-20", "20-24"] time_slices = TimeSlice.where('name REGEXP ?', time_slice_names.join("|

在我的项目中,我有不同的时间片。这些时间片有“00-04”、“04-08”、“08-12”、“12-16”、“16-20”和“20-24”等名称。我想得到所有具有上述名称的时间片对象。因此,我编写了以下程序:

time_slice_names = ["00-04", "04-08", "08-12", "12-16", "16-20", "20-24"]
time_slices = TimeSlice.where('name REGEXP ?', time_slice_names.join("|"))
时间片在我的开发模式中为我提供了正确的对象。但是,当我运行测试时,出现以下错误:

 ActiveRecord::StatementInvalid:
 SQLite3::SQLException: no such function: REGEXP: SELECT "time_slices".* FROM "time_slices"  WHERE (name REGEXP '00-04|04-08|08-12|12-16|16-20|20-24')
My database.yml如下所示:

  development:
    adapter: mysql2
    database: development 
    encoding: utf8
    pool: 5
    timeout: 5000

  test:
    adapter: sqlite3
    database: db/test.sqlite3
    pool: 5
    timeout: 5000

我知道我的测试环境使用sqlite3,而我的开发环境使用mysql2。我是否可以在mysql2和sqlite3上执行上述查询

因此,根据中的不同答案,
regexp()
默认情况下不是在Sqlite3中定义的。您必须安装一个将此功能添加到Sqlite3的本机扩展

但是,查询可以在不使用正则表达式的情况下完成

time_slice_names = ["00-04", "04-08", "08-12", "12-16", "16-20", "20-24"]
time_slices = TimeSlice.where(name: time_slice_names)
内部数据库适配器(mysql或sqlite3)应该理解这一点,并将其转换为支持(x,y,z)查询中的
where字段


更多可用信息

我发现在dev/test/prod中使用相同的DB适配器更好。但是,请检查(忽略接受的答案)。@sadiksha Me也不太了解SQLite部分。因此,如果它与MySQL而不是SQLite一起工作,那么错误显然表明SQLite没有实现该regexp函数。所以最好像ndn建议的那样对所有环境使用相同的Db。谢谢你和@ndn,在这个例子中,我使用了查询来处理数组。我一定会考虑你的建议。@ SadikshaGautam最好是用你现在的解决方案发布一个答案,然后接受它,为将来的用户找到答案。