質問編集履歴
4
修正依頼のため。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,12 +1,156 @@
|
|
1
1
|
使用しているOSはOS Xで、以下のコードはRailsのRakefile上に書いております。
|
2
2
|
|
3
3
|
本題ですが以下の様なハッシュオブジェクト(多次元ハッシュ?)があり、この各々の値(要素)をDBへ入れたいです。
|
4
|
+
|
5
|
+
APIの呼び出し元は以下のサイトです
|
6
|
+
|
7
|
+
[APIドキュメント、EVENTS項目](https://apifootball.com/documentation/#Events)
|
4
8
|
|
5
9
|
|
6
10
|
|
7
11
|
```ここに言語を入力
|
8
12
|
|
13
|
+
#rakefile
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
def latest_game(url)
|
18
|
+
|
19
|
+
away = nil
|
20
|
+
|
21
|
+
home = nil
|
22
|
+
|
23
|
+
ex_ah = nil
|
24
|
+
|
25
|
+
json_data = Net::HTTP.get(URI.parse(url))
|
26
|
+
|
27
|
+
data = JSON.parse(json_data)
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
#入れ子になっているlineup以下のハッシュの値を変数に入れる
|
32
|
+
|
33
|
+
data.each do |detail2|
|
34
|
+
|
35
|
+
b = detail['lineup']
|
36
|
+
|
37
|
+
home = b.assoc('home').pop
|
38
|
+
|
39
|
+
away = b.assoc('away').pop
|
40
|
+
|
9
|
-
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
#lineup以外をデータに入れる
|
46
|
+
|
47
|
+
team_data.each do |detail3|
|
48
|
+
|
49
|
+
ex_ah = detail2.except('lineup')
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
#上のハッシュをまとめる
|
56
|
+
|
57
|
+
summary = ex_ah.zip(home,away)
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
#3つの多次元ハッシュがまとめられた変数summaryをレシーバにeachメソッドでtablesテーブルに値を入れたい
|
62
|
+
|
63
|
+
summary.each do |detail1|
|
64
|
+
|
65
|
+
Table.create(match_id:detail1['match_id'], country_id:detail1['country_id'],
|
66
|
+
|
67
|
+
country_name:detail1['country_name'], league_id:detail1['league_id'], league_name:detail1['league_name'],
|
68
|
+
|
69
|
+
match_date:detail1['match_date'], match_status:detail1['match_status'], match_time:detail1['match_time'],
|
70
|
+
|
71
|
+
match_hometeam_name:detail1['match_hometeam_name'], match_hometeam_score:detail1['match_hometeam_score'],
|
72
|
+
|
73
|
+
match_awayteam_name:detail1['match_awayteam_score'], match_awayteam_score:detail1['match_awayteam_score'],match_hometeam_halftime_score:detail1['match_hometeam_halftime_score'],
|
74
|
+
|
75
|
+
match_awayteam_halftime_score:detail1['match_awayteam_halftime_score'],
|
76
|
+
|
77
|
+
match_hometeam_extra_score:detail1['match_hometeam_extra_score'],
|
78
|
+
|
79
|
+
match_awayteam_extra_score:detail1['match_awayteam_extra_score'],
|
80
|
+
|
81
|
+
match_hometeam_penalty_score:detail1['match_hometeam_penalty_score'],
|
82
|
+
|
83
|
+
match_awayteam_penalty_score:detail1['match_awayteam_penalty_score'],
|
84
|
+
|
85
|
+
match_hometeam_system:detail1['match_hometeam_system'],
|
86
|
+
|
87
|
+
match_awayteam_system:detail1['match_awayteam_system'],
|
88
|
+
|
89
|
+
match_live:detail1['match_live'],
|
90
|
+
|
91
|
+
goalscorer:detail1['goalscorer'],
|
92
|
+
|
93
|
+
cards:detail1['cards'],
|
94
|
+
|
95
|
+
sub_h:detail1['substitutes'],
|
96
|
+
|
97
|
+
sub_a:detail1['substitutes'],
|
98
|
+
|
99
|
+
coach_h:detail1['coach'], coach_a:detail1['coach'],
|
100
|
+
|
101
|
+
substitute_h:detail1['substitutions'],
|
102
|
+
|
103
|
+
substitute_a:detail1['substitutions'],
|
104
|
+
|
105
|
+
lineup_h:detail1['starting_lineups'],
|
106
|
+
|
107
|
+
lineup_a:detail1['starting_lineups'],
|
108
|
+
|
109
|
+
shotson_h:detail1['statistics'])
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
namespace :latest_game_l do
|
118
|
+
|
119
|
+
desc '最新試合日程取得'
|
120
|
+
|
121
|
+
task :get_latest_game => :environment do
|
122
|
+
|
123
|
+
day = Date.today
|
124
|
+
|
125
|
+
days = day.to_s
|
126
|
+
|
127
|
+
tommorow = day + 6
|
128
|
+
|
129
|
+
tommorows = tommorow.to_s
|
130
|
+
|
131
|
+
latest_game('https://apifootball.com/api/?action=get_events&from=' + days + '&to=' + tommorows + '&league_id=62&APIkey=xxxxxxxxxxxx')
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
```ここに言語を入力
|
152
|
+
|
153
|
+
#1.変数dataの中身
|
10
154
|
|
11
155
|
data=
|
12
156
|
|
@@ -24,105 +168,25 @@
|
|
24
168
|
|
25
169
|
```
|
26
170
|
|
27
|
-
しかし今、個人では以
|
171
|
+
しかし今、個人では以上の様に実装しているのですがうまくいきません。
|
28
172
|
|
29
173
|
lineupのkeyであるhomeとawayが入れ子になっている為、まずhomeとawayの値を変数として持っています。
|
30
174
|
|
31
175
|
また、homeとawayの重複を防ぐ為、homeとaway(lineup=>)以外のハッシュも変数として持っています
|
32
176
|
|
33
|
-
|
177
|
+
dataの中身をみるとわかる様に、多次元のハッシュになっている為、このex_ah、home、awayを一つにまとめ、テーブルに入れる為zipメソッドを使おうと使いました。
|
34
178
|
|
35
|
-
#2
|
36
|
-
|
37
|
-
away = nil
|
38
|
-
|
39
|
-
home = nil
|
40
|
-
|
41
|
-
ex_ah
|
179
|
+
そもそもaway,home,ex_ahの様なブロック内で再代入した変数をブロック外で呼び出すと繰り返し処理を行う訳ではないので一つの要素しか呼ばれないとは思いますが。。。
|
42
180
|
|
43
181
|
|
44
182
|
|
45
|
-
|
183
|
+
以上の様なコードで、プロジェクトがあるディレクトリでrakeコマンドを実行してみるとno implicit conversion of String into Integer等と怒られます。
|
46
184
|
|
47
|
-
b = detail['lineup']
|
48
|
-
|
49
|
-
|
185
|
+
本当は上の様に既に作っているモデル、テーブルに値を入れていきたいのですが、、、、
|
50
|
-
|
51
|
-
away = b.assoc('away').pop
|
52
|
-
|
53
|
-
end
|
54
186
|
|
55
187
|
|
56
188
|
|
57
|
-
data.each do |detail2|
|
58
|
-
|
59
|
-
except_ah = detail2.except('lineup')
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
```
|
64
|
-
|
65
|
-
dataの中身をみるとわかる様に、多次元のハッシュになっている為、このex_ah、home、awayを一つにまとめ、テーブルに入れる為zipメソッドを使おうと使いました。
|
66
|
-
|
67
|
-
```ここに言語を入力
|
68
|
-
|
69
|
-
array = ex_ah.zip(home,away)
|
70
|
-
|
71
|
-
puts arrray
|
72
|
-
|
73
|
-
```
|
74
|
-
|
75
|
-
とりあえずこのarrayの中身を確認する為に、puts arrayと出力してみるとコンソール上には
|
76
|
-
|
77
|
-
```ここに言語を入力
|
78
|
-
|
79
|
-
#3
|
80
|
-
|
81
|
-
starting_lineups
|
82
|
-
|
83
|
-
starting_lineups
|
84
|
-
|
85
|
-
country_id
|
86
|
-
|
87
|
-
169
|
88
|
-
|
89
|
-
country_name
|
90
|
-
|
91
|
-
England
|
92
|
-
|
93
|
-
```
|
94
|
-
|
95
|
-
の様にハッシュでも配列でもないようなログが返ってきました、しかも#3の様にdataの中にある複数のハッシュオブジェクトの中でまとめられた一つのものしか返ってきません。そもそもaway,home,ex_ahの様なブロック内で再代入した変数をブロック外で呼び出すと一つの要素しか呼ばれないですけど。。。
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
変数arrayを
|
100
|
-
|
101
|
-
```ここに言語を入力
|
102
|
-
|
103
|
-
#4
|
104
|
-
|
105
|
-
array.each do |arr|
|
106
|
-
|
107
|
-
puts array.arr['country_id']
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
#例 Table.create(country_id:arr['country_id'],country_name:arr['country_name'])
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
end
|
116
|
-
|
117
|
-
```
|
118
|
-
|
119
|
-
の様にしてもno implicit conversion of String into Integerと怒られます。
|
120
|
-
|
121
|
-
本当は上のコメントアウト箇所の様に既に作っているテーブルに値を入れていきたいのですが、、、、
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
長くなりましたが、端的な質問をまとめると、タイトルにも書きましたがネストしたハッシュの各々の値をどの様にすれば、DBのテーブルの同じ行に入れていけるのかということです。
|
189
|
+
長くなりましたが、端的な質問をまとめると、タイトルにも書きましたがネストしたハッシュの各々の値をどの様にすれば、DBのテーブルの同じ行に入れていけるのかということです。同じテーブルの同じ行に要素を入れていきたいです。
|
126
190
|
|
127
191
|
何日も格闘しているのですが全く解決できなかった為、質問いたしました。
|
128
192
|
|
3
修正依頼
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,39 +10,17 @@
|
|
10
10
|
|
11
11
|
data=
|
12
12
|
|
13
|
-
{
|
13
|
+
{"match_id"=>"306341", "country_id"=>"169", "country_name"=>"England", "league_id"=>"62", "league_name"=>"Premier League", "match_date"=>"2018-09-02", "match_status"=>"", "match_time"=>"14:30", "match_hometeam_name"=>"Cardiff City", "match_hometeam_score"=>"", "match_awayteam_name"=>"Arsenal", "match_awayteam_score"=>"", "match_hometeam_halftime_score"=>"", "match_awayteam_halftime_score"=>"", "match_hometeam_extra_score"=>"", "match_awayteam_extra_score"=>"", "match_hometeam_penalty_score"=>"", "match_awayteam_penalty_score"=>"", "match_hometeam_system"=>"", "match_awayteam_system"=>"", "match_live"=>"0", "goalscorer"=>[], "cards"=>[], "lineup"=>{"home"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}, "away"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}}, "statistics"=>[]}
|
14
14
|
|
15
|
-
{"id"=>"
|
15
|
+
{"match_id"=>"306342", "country_id"=>"169", "country_name"=>"England", "league_id"=>"62", "league_name"=>"Premier League", "match_date"=>"2018-09-02", "match_status"=>"", "match_time"=>"17:00", "match_hometeam_name"=>"Watford", "match_hometeam_score"=>"", "match_awayteam_name"=>"Tottenham Hotspur", "match_awayteam_score"=>"", "match_hometeam_halftime_score"=>"", "match_awayteam_halftime_score"=>"", "match_hometeam_extra_score"=>"", "match_awayteam_extra_score"=>"", "match_hometeam_penalty_score"=>"", "match_awayteam_penalty_score"=>"", "match_hometeam_system"=>"", "match_awayteam_system"=>"", "match_live"=>"0", "goalscorer"=>[], "cards"=>[], "lineup"=>{"home"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}, "away"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}}, "statistics"=>[]}
|
16
16
|
|
17
|
-
"lineup"=>{
|
17
|
+
{"match_id"=>"310833", "country_id"=>"169", "country_name"=>"England", "league_id"=>"62", "league_name"=>"Premier League", "match_date"=>"2018-09-02", "match_status"=>"", "match_time"=>"17:00", "match_hometeam_name"=>"Burnley", "match_hometeam_score"=>"", "match_awayteam_name"=>"Manchester United", "match_awayteam_score"=>"", "match_hometeam_halftime_score"=>"", "match_awayteam_halftime_score"=>"", "match_hometeam_extra_score"=>"", "match_awayteam_extra_score"=>"", "match_hometeam_penalty_score"=>"", "match_awayteam_penalty_score"=>"", "match_hometeam_system"=>"", "match_awayteam_system"=>"", "match_live"=>"0", "goalscorer"=>[], "cards"=>[], "lineup"=>{"home"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}, "away"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}}, "statistics"=>[]}
|
18
18
|
|
19
|
-
"home"=>{"starting_lineups"=>[], "substitutions"=>[]},
|
19
|
+
{"match_id"=>"317207", "country_id"=>"169", "country_name"=>"England", "league_id"=>"62", "league_name"=>"Premier League", "match_date"=>"2018-09-02", "match_status"=>"", "match_time"=>"17:00", "match_hometeam_name"=>"Watford FC", "match_hometeam_score"=>"", "match_awayteam_name"=>"Tottenham Hotspur", "match_awayteam_score"=>"", "match_hometeam_halftime_score"=>"", "match_awayteam_halftime_score"=>"", "match_hometeam_extra_score"=>"", "match_awayteam_extra_score"=>"", "match_hometeam_penalty_score"=>"", "match_awayteam_penalty_score"=>"", "match_hometeam_system"=>"", "match_awayteam_system"=>"", "match_live"=>"0", "goalscorer"=>[], "cards"=>[], "lineup"=>{"home"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}, "away"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}}, "statistics"=>[]}
|
20
20
|
|
21
|
-
"away"=>{"starting_lineups"=>[], "substitutions"=>[]}
|
22
21
|
|
23
|
-
},
|
24
22
|
|
25
|
-
"statistics"=>[]},
|
26
23
|
|
27
|
-
.
|
28
|
-
|
29
|
-
.省略
|
30
|
-
|
31
|
-
.
|
32
|
-
|
33
|
-
{"id"=>"20", "country_id"=>"169", "country_name"=>"England", "league_id"=>"62",
|
34
|
-
|
35
|
-
"lineup"=>{
|
36
|
-
|
37
|
-
"home"=>{"starting_lineups"=>[], "substitutions"=>[]},
|
38
|
-
|
39
|
-
"away"=>{"starting_lineups"=>[], "substitutions"=>[]}
|
40
|
-
|
41
|
-
},
|
42
|
-
|
43
|
-
"statistics"=>[]}
|
44
|
-
|
45
|
-
}
|
46
24
|
|
47
25
|
```
|
48
26
|
|
2
過不足為。
test
CHANGED
File without changes
|
test
CHANGED
@@ -90,6 +90,8 @@
|
|
90
90
|
|
91
91
|
array = ex_ah.zip(home,away)
|
92
92
|
|
93
|
+
puts arrray
|
94
|
+
|
93
95
|
```
|
94
96
|
|
95
97
|
とりあえずこのarrayの中身を確認する為に、puts arrayと出力してみるとコンソール上には
|
1
過不足があった為。
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
|
6
6
|
|
7
7
|
```ここに言語を入力
|
8
|
+
|
9
|
+
#1
|
8
10
|
|
9
11
|
data=
|
10
12
|
|
@@ -51,6 +53,8 @@
|
|
51
53
|
また、homeとawayの重複を防ぐ為、homeとaway(lineup=>)以外のハッシュも変数として持っています
|
52
54
|
|
53
55
|
```ここに言語を入力
|
56
|
+
|
57
|
+
#2
|
54
58
|
|
55
59
|
away = nil
|
56
60
|
|
@@ -116,6 +120,8 @@
|
|
116
120
|
|
117
121
|
```ここに言語を入力
|
118
122
|
|
123
|
+
#4
|
124
|
+
|
119
125
|
array.each do |arr|
|
120
126
|
|
121
127
|
puts array.arr['country_id']
|