Ruby on rails 4 JRuby-将查询结果转换为字符串而不是日期
以下方法调用在使用ruby 2.3.3时返回日期,但在使用带有activerecord jdbcpostgresql适配器的JRuby-9.1.17.0时返回字符串:Ruby on rails 4 JRuby-将查询结果转换为字符串而不是日期,ruby-on-rails-4,jdbc,activerecord,jruby,jrubyonrails,Ruby On Rails 4,Jdbc,Activerecord,Jruby,Jrubyonrails,以下方法调用在使用ruby 2.3.3时返回日期,但在使用带有activerecord jdbcpostgresql适配器的JRuby-9.1.17.0时返回字符串: activerecord 4.2.11.1 activerecord-jdbc-adapter 1.3.25 activerecord-jdbcpostgresql-adapter 1.3.25 jdbc-postgres 9.4.1206 仅当数据库中不存在所选属性时才会发生此情况: jruby-9.1.17.0 :002 &
activerecord 4.2.11.1
activerecord-jdbc-adapter 1.3.25
activerecord-jdbcpostgresql-adapter 1.3.25
jdbc-postgres 9.4.1206
仅当数据库中不存在所选属性时才会发生此情况:
jruby-9.1.17.0 :002 > Table.select('now() as date').first.date
Table Load (2.0ms) SELECT now() as date FROM "tables" ORDER BY "tables"."id" ASC LIMIT 1
=> "2019-05-17 03:48:57.572526+00"
WebRick或Tomcat7都会出现这种情况。如果使用activerecord 4.0或4.1,则同一版本的activerecord jdbc适配器不会发生此错误。升级到最新的jruby-9.2.7.0也无济于事。似乎是AR-JDBC中的一个兼容性错误,此时可能无法修复,因为AR 4.2也不受支持 如果这是一个问题,您应该尝试升级到5.x,或者尝试解决该问题并提交一份PR
最后尝试设置
ActiveRecord::ConnectionAdapters::JdbcConnection.raw_date\u time=true
(尽管我认为在AR 4.2上运行时它已经是true
)正如@kares所指出的,这看起来像一个bug,而且它与配置ActiveRecord::ConnectionAdapters::JdbcConnection.raw_date\u time
有关。如果设置为true,则下面的方法调用将返回字符串,否则将返回时间实例:
jruby-9.1.17.0 :019 > Table.select(:created_at).first.created_at
Table Load (0.8ms) SELECT "tables"."created_at" FROM "tables" ORDER BY "tables"."id" ASC LIMIT 1
=> Wed, 25 Sep 2013 14:26:17 -03 -03:00
我已使用3个版本的active\u record
检查了此行为:
Locatario.select('now() as date').first.date
因此,它看起来像是在模拟active\u record>=4.0
gemactiverecord jdbc适配器
应该使用raw\u date\u time=false
,因为它应该返回一个带有方法调用的时间实例。作为一种解决方法,我在initializers下创建了一个包含以下内容的文件:
Rails 3.2
default raw_date_time? == true
Table.select('now() as date').first.date
Ruby: returns String
JRuby: returns String
Rails 4.0/4.1
default raw_date_time? == nil
Table.select('now() as date').first.date
Ruby: returns Time
JRuby: returns Time
Rails 4.2
default raw_date_time? == true
Table.select('now() as date').first.date
Ruby: returns Time
JRuby: returns String
你好,卡里斯。事实上,在Rails 4.0中,raw_date_time为零,并且它的行为正确。对于rails 4.2,raw_date_time为true,除了我在文章中提到的情况外,它的行为是正确的。但如果我将其更改为false,则它适用于这种情况。原始日期时间是多少?==真的意思?它可能会打破其他情况,但目前还不完全确定,但它是在4.x/4.2支持范围内添加的,因此它必须与AR兼容性相关,尤其是在4.2上的
true
。。。您可以尝试运行一些测试和其他适配器,如MySQL
Locatario.select('now() as date').first.date
Rails 3.2
default raw_date_time? == true
Table.select('now() as date').first.date
Ruby: returns String
JRuby: returns String
Rails 4.0/4.1
default raw_date_time? == nil
Table.select('now() as date').first.date
Ruby: returns Time
JRuby: returns Time
Rails 4.2
default raw_date_time? == true
Table.select('now() as date').first.date
Ruby: returns Time
JRuby: returns String
if RUBY_PLATFORM == "java" && Rails::VERSION::STRING.starts_with?("4.2.")
ActiveRecord::ConnectionAdapters::JdbcConnection.raw_date_time = false
end