我有5个字段,其中4个字段应该在一个表中,最后一列应该在另一个表中导入
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
lock_store_hash = row.to_hash
lock_store = LockStore.where(id: lock_store_hash["id"])
if lock_store.count == 1
lock_store.first.update_attributes(lock_store_hash.except(params[:file][:taxon_id]))
else
LockStore.create!(lock_store_hash)
end
end
end
答案 0 :(得分:0)
是的,你可以,在你的情况下,你可以做这样的事情(我假设你的模特之间有一个belongs_to和has_many):
...
if lock_store.count == 1
lock_store.first.update_attributes(lock_store_hash.except(params[:file][:taxon_id]))
# the update code I will let to you because is difficult to assume based in the information provided
else
lockstore = LockStore.create!(lock_store_hash)
lockstore.another_models.create(attribute_name: lock_store_hash[:last_column])
end