[英]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.