简体   繁体   English

Rails 4 model.where返回JSON而不是哈希

[英]Rails 4 model.where returning JSON instead of hash

I have a list of contacts in my database. 我的数据库中有一个contacts列表。 I am calling them like so: 我这样称呼他们:

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

contacts[0] returns the following: 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>

I'm then trying to loop through all contacts in my Sidekiq worker: 然后,我试图遍历我的Sidekiq工作者中的所有contacts

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

This throws the following error: 这将引发以下错误:

 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>

So, it looks like my contacts array is actually returning an array of JSON objects instead of a hash. 因此,看来我的contacts数组实际上返回的是JSON对象数组,而不是哈希值。 I don't think I've ever seen this, it seems that I always receive a hash when calling Model.where() . 我认为我从未见过,似乎在调用Model.where()时总是收到哈希值。

What is causing this, and how can I loop through contacts within my worker like I need to? 是什么原因造成的,以及如何像我需要的那样循环遍历我的工作人员中的contacts

EDIT 编辑

Full class below: 完整课程如下:

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

Sidekiq serialize passed params to store they as redis, and I thing that problem of you serializer. Sidekiq对传递的参数进行序列化以将它们存储为Redis,而我觉得序列化器存在问题。

I recommend you follow this document a Best Practices and rewrite code: 我建议您按照本文档的“最佳实践”并重写代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM