我正在將Rails 3應用程序升級到Rails 4.在Rails 3中,包含ActiveRecord對象數組的哈希序列化在json中正常工作;現在在Rails 4中它有不可預知的結果。Rails 4 to_json產生意外的異常nil不是符號
下面是一個例子對象與TypeError Exception: nil is not a symbol
失敗上軌道4
{1230 =>
[
#<QuestionAnswerResponse response_id: 127, response_set_id: 6, response_group: nil, question_data_export_identifier: "is_co_pi_involved", answer: "No", question_id: 1230, answer_id: 2077, response: "">,
#<QuestionAnswerResponse response_id: 131, response_set_id: 6, response_group: nil, question_data_export_identifier: "is_co_pi_involved", answer: "No", question_id: 1230, answer_id: 2077, response: "">
]
}
現在,如果我採取另一種相似對象;含哈希ActiveRecord對象的數組和運行to_json它適用於這一個...
{1234 =>
[
#<Response id: 1, response_set_id: 2, question_id: 4, answer_id: 2, datetime_value: nil, integer_value: nil, float_value: nil, unit: nil, text_value: nil, string_value: nil, response_other: nil, response_group: nil, created_at: "2014-05-30 21:17:23", updated_at: "2014-05-30 21:17:23", survey_section_id: 1, api_id: "f44b22ba-a93b-477f-8a7f-c5b4566338f0", attach_file_name: nil, attach_content_type: nil, attach_file_size: nil, attach_updated_at: nil>,
#<Response id: 2, response_set_id: 2, question_id: 10, answer_id: 10, datetime_value: nil, integer_value: nil, float_value: nil, unit: nil, text_value: "test", string_value: nil, response_other: nil, response_group: nil, created_at: "2014-05-30 21:17:23", updated_at: "2014-05-30 21:17:23", survey_section_id: 1, api_id: "e7fa8aa2-6e47-4f88-8802-949fdc902a2e", attach_file_name: nil, attach_content_type: nil, attach_file_size: nil, attach_updated_at: nil>
]
}
將'self.primary_key =:id#或其他主鍵'添加到模型中。謝謝! – Oleander
非常感謝,夥計們! – itsnikolay
非常有幫助,謝謝。我有一個沒有主鍵的遺留表,所以我只是使用[composite_primary_keys](https://github.com/composite-primary-keys/composite_primary_keys)來指定應該是唯一的列組合。 –