[英]Import data from excel file in to model with validations and associations in rails
如何使用导入数据进行模型关联
这是我的情况:
class SampleRequest < ActiveRecord::Base
has_one :sample_request_text_excerpt
accepts_nested_attributes_for :sample_request_text_excerpt
end
class SampleRequestTextExcerpt < ActiveRecord::Base
belongs_to :sample_request
end
在sample_request模型中,资产类型字段是text和image,对于文本类型不同的excel表格,对于图像类型不同的excel表格,sample_request_text_excerpt模型中只有某些字段是多余的。
对于单个模型没有问题,请尝试用于关联
此行显示错误:
sample_request.attributes = row.to_hash
上载显示的文本电子表格错误时显示。
在设置sample_request
对象的属性时,您正在使用哈希。 在该哈希中,有一个关键字stock_id
而当Rails尝试为您的sample_request
对象设置该属性时,它将找不到它。 这很可能是因为sample_request模型上没有stock_id
列。
为了解决这个问题,您要么需要:
1)从哈希中删除stock_id
键。 您可以使用.delete完成此操作。
2)或者,您可以将stock_id
添加到sample_request
模型中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.