Search code examples
ruby-on-railsruby-on-rails-3delayed-job

String value gets converted to Time object when a model method is called using delayed job


I am using delayed job to queue a Model method in another Model like this:

article_loader.rb

date_value_in_string =  "2017-06-21 07:17:00"
Article.delay(:queue => 'article_load').article_loading([date_value_in_string])

Even though I have passed a String as an argument to the method, inside the method it gets converted to Time object.

article.rb

def self.article_loading(args)
  date_value = args[0]
  p date_value.class # Time
end

I don't know why this happens. Any help will be appreciated.


Solution

  • This happens due to the fact the string looks like a datetime to Ruby's default YAML loader. DelayedJob uses YAML for serialization, so this aspect manifests.

    If you want to keep the string as string, stuff some padding chars to the beginning of it, like x or something. Demo follows.

    Consider this 1.yml:

    ds: 2017-06-21 07:17:00
    

    , then, in IRB, do a:

    >> require "yaml"
    >> YAML.load_file("1.yml")
    => {"ds"=>2017-06-21 11:17:00 +0400}
    >> _["ds"].class
    => Time