简体   繁体   中英

Rails 4 model.where returning JSON instead of hash

I have a list of contacts in my database. I am calling them like so:

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

contacts[0] returns the following:

=> #<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:

  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. I don't think I've ever seen this, it seems that I always receive a hash when calling Model.where() .

What is causing this, and how can I loop through contacts within my worker like I need to?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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