这是我在Rails 5模型中使用数组的方式在迁移中
t.text :diagnoses, array: true, default: []
在模型中
class Patient < ApplicationRecord
serialize :diagnoses, Array
end
在我的种子方法中,我正在这样做
patient = Patient.create(first_name: 'John', last_name: 'Smith', admission: a)
patient.diagnoses = [1, 2]
patient.save!
它给出错误为
ActiveRecord::SerializationTypeMismatch: can't dump `diagnoses`: was supposed to be a Array, but was a Integer. -- 0
感谢您的帮助!
前一段时间,我遇到了这个确切的问题。我发现以下解决方法:
我会认真考虑实际上实际上是正确使用关系数据库。