Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

mysql - ArgumentError (hour out of range) in ActiveRecord

I have a problem with a legacy MySQL database that I'm not able to solve. I believe the culprit is that one of the fields has the wrong type set (it contains time in milliseconds, which the adapter doesn't expect having type DateTime), and unfortunately I can't change it. What happens is this (using Ruby on Jets):

2.5.8 :001 > User.all.each {|u| puts u.inspect};mil
Traceback (most recent call last):
       16: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `records'
       15: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `load'
       14: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `exec_queries'
       13: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `skip_query_cache_if_necessary'
       12: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `block in exec_queries'
       11: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `find_by_sql'
       10: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `select_all'
        9: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `select_all'
        8: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `select_all'
        7: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `select'
        6: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `exec_query'
        5: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `execute_and_free'
        4: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `block in exec_query'
        3: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `to_a'
        2: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `each'
        1: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord- `utc'
ArgumentError (hour out of range)

I've tried to monkey patch this but I don't think I'm patching the right thing, it looks like the thing to change is the adapter (ouch...). I've seen other changed ActiveSupport but it didn't work out for me, and basically I can't loop over my records without ActiveRecord crashing. I can't even rescue the problem, I don't know why but rescue ArgumentError doesn't get triggered by that exception. Has anyone encountered this problem and managed to disable AR parsing of the records? It would be enough to get the value that crashes AR and / 1000 but I apparently can't find a way to do this.


class User < ApplicationRecord
  self.table_name = :user
  self.primary_key = :user_id

  alias_attribute :first_name, 'user_firstname'
  alias_attribute :last_name, 'user_lastname'
  alias_attribute :email, 'user_email'
  alias_attribute :phone, 'user_contact_number'
  alias_attribute :job_title, 'user_job_title'
  alias_attribute :contact_status, 'user_contact_status'
  alias_attribute :created_at, 'user_created'

  # has_one :settings, class_name: 'UserSettings'
  # has_many :cases, foreign_key: :created_by_user_id
  # has_many :subscriptions, -> { order('id DESC') }
  # has_many :orders, foreign_key: :user_id

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

547k answers


35.3k users