0
データのCSVをインポートしていますが、現在のユーザーデータをすべて削除してからインポートしたいと思います。すべての現在のユーザーのデータを新しくアップロードされたデータで効果的に置き換えます。私はそれを理解できません。どんな助けでも大歓迎です。ここで import csv既存のレコードをすべて削除してからインポートする
は..私はそれがすべてのユーザーデータだけではなく、現在のユーザーのデータを削除:(作業doestれ、持っているものであるcurrent_user.idclass Inventory < ApplicationRecord
belongs_to :user
def self.import(file, user_id)
Inventory.destroy_all
allowed_attributes = [ "user_id", "id","description","part_number","price","created_at","updated_at", "alternate_part_number", "condition_code", "qty", "mfg_code", "serial_number", "part_comments"]
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
inventory = find_by_id(row["id"]) || new
inventory.attributes = row.to_hash.select { |k,v| allowed_attributes.include? k }
inventory.user_id = user_id
inventory.save!
end
end
ちょっとした注意:在庫が常に1人のユーザにインポートされる場合、 'import'は' User'クラスのインスタンスメソッドでなければなりません。 'Inventory'のクラスメソッドではありません。また、 'Inventory'か' InventoryItem'ですか? –