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和activerecord适配器时返回一个日期,但在使用ruby2.3.3时返回一个字符串:
2.3.3 :017 > Table.select('now() as date').first.date
Table Load (0.7ms) SELECT now() as date FROM "tables" ORDER BY "tables"."id" ASC LIMIT 1
=> 2019-05-17 03:46:52 UTC 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" 只有在数据库:中不存在所选属性时才会发生这种情况:
2.3.3 :012 > Table.select(:created_at).first.created_at
Table Load (0.6ms) SELECT "tables"."created_at" FROM "tables" ORDER BY "tables"."id" ASC LIMIT 1
=> Wed, 25 Sep 2013 14:26:17 -03 -03:00 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 这发生在WebRick或Tomcat 7中。如果使用Active Record 4.0或4.1,则同一版本的activerecord适配器不会出现此错误。升级到最新的jruby-9.2.7.0也于事无补。
发布于 2019-05-17 07:35:56
似乎是AR-JDBC中的兼容性缺陷,此时可能不会修复,因为也不支持AR 4.2。
如果这是一个问题,您应该尝试升级到5.x,或者尝试解决问题并提交一个PR .
作为最后的手段,尝试设置ActiveRecord::ConnectionAdapters::JdbcConnection.raw_date_time = true (我认为在AR 4.2上运行时它已经是true了)
发布于 2019-06-05 04:23:27
正如@kares所指出的,这看起来像一个bug,也与配置ActiveRecord::ConnectionAdapters::JdbcConnection.raw_date_time有关。如果它设置为true,下面的方法调用将返回一个字符串,否则它将返回一个Time实例:
Locatario.select('now() as date').first.date我已经用3个版本的active_record检查了这种行为
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因此,它看起来像模拟active_record >= 4.0一样-- gem activerecord-jdbc-adapter应该使用raw_date_time = false,因为它应该返回一个带有有关方法调用的时间实例。作为一种解决办法,我创建了一个文件,文件的初始化器下面有以下内容:
if RUBY_PLATFORM == "java" && Rails::VERSION::STRING.starts_with?("4.2.")
ActiveRecord::ConnectionAdapters::JdbcConnection.raw_date_time = false
endhttps://stackoverflow.com/questions/56140906
复制相似问题