簡體   English   中英

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

[英]Rails 4 model.where returning JSON instead of 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

Sidekiq對傳遞的參數進行序列化以將它們存儲為Redis,而我覺得序列化器存在問題。

我建議您按照本文檔的“最佳實踐”並重寫代碼:

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