質問編集履歴
1
説明がわかりにくかったので書き換えました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
railsでグループ化した情報を出力する方法
|
1
|
+
railsでeachで一定の値でグループ化?した情報を出力する方法
|
test
CHANGED
@@ -4,45 +4,37 @@
|
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
-
駅
|
7
|
+
都道府県、駅、路線名が別々もモデルになっておりアソシエーションを組んでおります。
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
---
|
12
|
-
|
13
|
-
stasion(駅名)のテーブルには
|
14
|
-
|
15
|
-
id
|
16
|
-
|
17
|
-
station_name|エキメイ
|
18
|
-
|
19
|
-
line_id|路線のID
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
---
|
24
|
-
|
25
|
-
line(路線名)のテーブルには
|
26
|
-
|
27
|
-
id
|
28
|
-
|
29
|
-
station_name|エキメイ
|
30
|
-
|
31
|
-
line_id|路線のID
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
---
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
があります。
|
40
8
|
|
41
9
|
|
42
10
|
|
43
11
|
```rugy
|
44
12
|
|
13
|
+
#########モデル#########
|
14
|
+
|
15
|
+
#pref.rb(都道府県)
|
16
|
+
|
17
|
+
class Pref < ApplicationRecord
|
18
|
+
|
19
|
+
has_many :stations
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
#station.rb(駅名)
|
26
|
+
|
27
|
+
class Station < ApplicationRecord
|
28
|
+
|
29
|
+
belongs_to :pref
|
30
|
+
|
31
|
+
belongs_to :line, foreign_key: 'line_id', primary_key: 'line_id'
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
|
45
|
-
#line.rb
|
37
|
+
#line.rb(路線名)
|
46
38
|
|
47
39
|
class Line < ApplicationRecord
|
48
40
|
|
@@ -58,11 +50,25 @@
|
|
58
50
|
|
59
51
|
```rugy
|
60
52
|
|
53
|
+
#########コントローラー#########
|
54
|
+
|
61
|
-
#station.rb
|
55
|
+
#stations_controller.rb
|
62
|
-
|
56
|
+
|
63
|
-
class Station < Application
|
57
|
+
class StationsController < ApplicationController
|
58
|
+
|
64
|
-
|
59
|
+
def index
|
60
|
+
|
65
|
-
|
61
|
+
@pref_name = Station.find_by(pref_id: params[:area_id])
|
62
|
+
|
63
|
+
@stations = Station.where(pref_id: params[:area_id])
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
def show
|
70
|
+
|
71
|
+
end
|
66
72
|
|
67
73
|
|
68
74
|
|
@@ -72,136 +78,150 @@
|
|
72
78
|
|
73
79
|
```
|
74
80
|
|
75
|
-
|
81
|
+
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
+
|
82
|
-
|
83
|
-
```ru
|
83
|
+
```rugy
|
84
|
+
|
84
|
-
|
85
|
+
#########ビュー#########
|
86
|
+
|
87
|
+
#stations/index.html.erb
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
<div class="container">
|
92
|
+
|
93
|
+
<div class="row">
|
94
|
+
|
95
|
+
<div class="col-md-12">
|
96
|
+
|
97
|
+
<h1><%= @pref_name.pref.name %>|<%= @stations.count %>駅|Stations#index</h1>
|
98
|
+
|
99
|
+
<p>Find me in app/views/stations/index.html.erb</p>
|
100
|
+
|
101
|
+
|
102
|
+
|
85
|
-
|
103
|
+
<% @stations.each do |station| %>
|
86
|
-
|
87
|
-
|
104
|
+
|
88
|
-
|
89
|
-
@pref_name = Station.find_by(pref_id: params[:area_id])
|
90
|
-
|
91
|
-
|
105
|
+
<div class="panel panel-default">
|
106
|
+
|
92
|
-
|
107
|
+
<div class="panel-heading"><%= station.line.line_name %></div>
|
108
|
+
|
109
|
+
<ul class="list-group">
|
110
|
+
|
111
|
+
<li class="list-group-item"><%= station.station_name %></li>
|
112
|
+
|
113
|
+
</ul>
|
114
|
+
|
93
|
-
|
115
|
+
</div>
|
94
|
-
|
95
|
-
|
96
|
-
|
116
|
+
|
97
|
-
d
|
117
|
+
<% end %>
|
98
|
-
|
118
|
+
|
99
|
-
|
119
|
+
</div>
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
120
|
+
|
105
|
-
|
121
|
+
</div>
|
122
|
+
|
123
|
+
</div>
|
106
124
|
|
107
125
|
|
108
126
|
|
109
127
|
```
|
110
128
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
で
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
129
|
+
|
130
|
+
|
131
|
+
DBはmysqlで
|
132
|
+
|
133
|
+
prefsテーブル
|
134
|
+
|
135
|
+
---------------
|
136
|
+
|
137
|
+
id:integer
|
138
|
+
|
139
|
+
pref_cd:integer
|
140
|
+
|
141
|
+
name:string
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
stationsテーブル
|
146
|
+
|
147
|
+
---------------
|
148
|
+
|
149
|
+
id:integer
|
150
|
+
|
151
|
+
pref_id:integer
|
152
|
+
|
153
|
+
station_cd:integer
|
154
|
+
|
155
|
+
station_name:string
|
156
|
+
|
157
|
+
line_id:integer
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
linesテーブル
|
162
|
+
|
163
|
+
---------------
|
164
|
+
|
165
|
+
id:integer
|
166
|
+
|
167
|
+
line_id:integer
|
168
|
+
|
169
|
+
line_name:string
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
現状は下記のような表示になってしまっています。
|
176
|
+
|
177
|
+

|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
- 【山手線】
|
182
|
+
|
183
|
+
- 大崎
|
184
|
+
|
185
|
+
- 【山手線】
|
186
|
+
|
187
|
+
- 五反田
|
188
|
+
|
189
|
+
- 【山手線】
|
190
|
+
|
191
|
+
- 目黒
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
上記のようになっているのを…
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
- 【山手線】
|
200
|
+
|
201
|
+
- 大崎
|
202
|
+
|
203
|
+
- 五反田
|
204
|
+
|
205
|
+
- 目黒
|
206
|
+
|
207
|
+
- 山手線の駅が全部出力されると…
|
208
|
+
|
209
|
+
- 【南武線】
|
210
|
+
|
211
|
+
- 矢野口
|
212
|
+
|
213
|
+
- 稲城長沼
|
214
|
+
|
215
|
+
- 南多摩
|
216
|
+
|
217
|
+
と言った具合に路線でグルーピング?して出力したいです。
|
218
|
+
|
219
|
+
各駅が持っているline_id(線路のID)をグループ化してどうにかしてeachで回せないかと考えていますが答えがでません。
|
220
|
+
|
221
|
+
どのように処理すれば解決しますでしょうか???
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
ぜひともご教授お願い致します!!!
|