Rails 4 model.where返回JSON而不是hash

时间:2014-03-18 13:21:56

标签: ruby-on-rails ruby json hash

我的数据库中有contacts列表。我这样称呼他们:

contacts = Contact.where(loaded_at: nil, failed_at: nil)

contacts[0]返回以下内容:

=> #<Contact id: 96, client_id: 2027, branch_id: 100, phone_type: "Mobile", unit_type: "Family", phone_number: "(123)456-7890", text_message_id: nil, loaded_at: nil, created_at: "2014-03-17 22:01:53", updated_at: "2014-03-17 22:01:53", failed_at: nil>

然后我试图遍历我的Sidekiq工作人员中的所有contacts

  def perform()
    contacts = Contact.where(loaded_at: nil, failed_at: nil)
    contacts.each { |contact| TextSendWorker.perform_async(contact) }
  end

这会引发以下错误:

 WARN: {"retry"=>true, "queue"=>"default", "class"=>"ContactWorker", "args"=>[], "jid"=>"3d3373e14f4bfe9ae2aa2cae", "enqueued_at"=>1395093915.4475722, "error_message"=>"undefined method `key?' for #<JSON::Ext::Generator::State:0x007f40b0bd42d0>", "error_class"=>"NoMethodError", "failed_at"=>1395093915.4909346, "retry_count"=>7, "retried_at"=>1395096684.1267653}
 WARN: undefined method `key?' for #<JSON::Ext::Generator::State:0x007f40b0bd42d0>

因此,看起来我的contacts数组实际上返回的是JSON个对象数组而不是哈希值。我不认为我曾经见过这个,似乎在调用Model.where()时我总是收到一个哈希。

造成这种情况的原因是什么,以及如何在我的工作人员中循环contacts,我需要这样做?

修改

以下全班:

class ContactWorker
  include Sidekiq::Worker

  def perform()
    contacts = Contact.where(loaded_at: nil, failed_at: nil)
    contacts.each { |contact| TextSendWorker.perform_async(contact) }
  end
end

1 个答案:

答案 0 :(得分:1)

Sidekiq序列化通过params将它们存储为redis,我认为你是串行器的问题。

我建议您按照此文档操作 a Best Practices 并重写代码:

class ContactWorker
  include Sidekiq::Worker

  def perform()
    contacts = Contact.where(loaded_at: nil, failed_at: nil)
    contacts.each { |contact| TextSendWorker.perform_async(contact.id) }
   end
 end