回答編集履歴
4
例の追加に伴い、説明を変更
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
配下の全レコードを集めるほうが、よりシンプルで、使い勝手も良さそう。
|
2
2
|
|
3
3
|
|
4
4
|
|
3
関数として実装した場合を追加
test
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
```ruby
|
6
|
+
|
7
|
+
# インスタンスメソッドとして実現
|
6
8
|
|
7
9
|
class Record
|
8
10
|
|
@@ -18,4 +20,24 @@
|
|
18
20
|
|
19
21
|
@item.all_records.map(&:id)
|
20
22
|
|
23
|
+
|
24
|
+
|
25
|
+
# 関数として実現
|
26
|
+
|
27
|
+
def collect_records(recode)
|
28
|
+
|
29
|
+
[
|
30
|
+
|
31
|
+
recode,
|
32
|
+
|
33
|
+
*recode.subs.flat_map { |sub_recode| collect_records(sub_recode) }
|
34
|
+
|
35
|
+
]
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
collect_records(@item).map(&:id)
|
42
|
+
|
21
43
|
```
|
2
select を削除
test
CHANGED
@@ -16,8 +16,6 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
-
@item.all_records
|
19
|
+
@item.all_records.map(&:id)
|
20
|
-
|
21
|
-
.select { |record| record.respond_to?(:id) }.map(&:id)
|
22
20
|
|
23
21
|
```
|
1
タイポを修正
test
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
def all_records
|
10
10
|
|
11
|
-
[self, *subs.flat_map(&:all_
|
11
|
+
[self, *subs.flat_map(&:all_records)]
|
12
12
|
|
13
13
|
end
|
14
14
|
|