回答編集履歴

1

追記

2020/08/14 04:21

投稿

winterboum
winterboum

スコア23567

test CHANGED
@@ -15,3 +15,41 @@
15
15
  @users = User.includes(:orders).all.
16
16
 
17
17
  にしないとDBに負担がかかります
18
+
19
+
20
+
21
+ 追記
22
+
23
+ ```
24
+
25
+ def self.csv_attributes
26
+
27
+ ["name", "tel", "email", "post_code", "prefectures",
28
+
29
+ "address_1", "address_2", "room_number", "created_at_date",
30
+
31
+ "updated_at_date", "orders_counts"]
32
+
33
+ end
34
+
35
+ def orders_counts
36
+
37
+ orders.count
38
+
39
+ end
40
+
41
+ ```
42
+
43
+ def self.generate_csv の方は
44
+
45
+ ```
46
+
47
+ joins(:orders).each do |user|
48
+
49
+ csv << csv_attributes.map{ |attr| user.send(attr) }
50
+
51
+ end
52
+
53
+ ```
54
+
55
+ でどうかな