質問するログイン新規登録

質問編集履歴

1

インポートコードの追加

2020/07/15 09:19

投稿

KOO_
KOO_

スコア58

title CHANGED
File without changes
body CHANGED
@@ -18,4 +18,19 @@
18
18
  hoge.industryが同一の値で、hoge.telが既に存在する場合は弾き、
19
19
  hoge.industryが別の値で、hoge.telが既に存在する場合はインポート
20
20
 
21
- するプログラムを組みたいのですが、どなたかご教示頂けないでしょうか?
21
+ するプログラムを組みたいのですが、どなたかご教示頂けないでしょうか?
22
+
23
+ ```
24
+ def self.import(file)
25
+ save_cont = 0
26
+ CSV.foreach(file.path, headers:true) do |row|
27
+ customer = find_by(id: row["id"]) || new
28
+ customer.attributes = row.to_hash.slice(*updatable_attributes)
29
+ next if self.where(tel: customer.tel, industry: customer.industry) > 0
30
+ next if self.where.not(industry: nil)
31
+ customer.save!
32
+ save_cnt += 1
33
+ end
34
+ save_cont
35
+ end
36
+ ```