質問編集履歴
1
中間テーブルを用意し、collection_check_boxesを使いました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -62,6 +62,106 @@
|
|
62
62
|
|
63
63
|
|
64
64
|
|
65
|
+
positionテーブル
|
66
|
+
|
67
|
+
---
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
class CreatePositions < ActiveRecord::Migration[5.1]
|
72
|
+
|
73
|
+
def change
|
74
|
+
|
75
|
+
create_table :positions do |t|
|
76
|
+
|
77
|
+
t.string :name
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
t.timestamps
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
中間テーブルのmember_position
|
94
|
+
|
95
|
+
---
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
class CreateMemberPositions < ActiveRecord::Migration[5.1]
|
100
|
+
|
101
|
+
def change
|
102
|
+
|
103
|
+
create_table :member_positions do |t|
|
104
|
+
|
105
|
+
t.string :member_id
|
106
|
+
|
107
|
+
t.string :position_id
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
t.timestamps
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
```
|
122
|
+
|
123
|
+
各モデルの設定
|
124
|
+
|
125
|
+
---
|
126
|
+
|
127
|
+
```
|
128
|
+
|
129
|
+
class Member < ApplicationRecord
|
130
|
+
|
131
|
+
has_many :member_positions
|
132
|
+
|
133
|
+
has_many :positions, through: :member_positions
|
134
|
+
|
135
|
+
accepts_nested_attributes_for :member_positions, allow_destroy: true
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
class Position < ApplicationRecord
|
142
|
+
|
143
|
+
has_many :member_positions
|
144
|
+
|
145
|
+
has_many :members, through: :member_positions
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
class MemberPosition < ApplicationRecord
|
152
|
+
|
153
|
+
belongs_to :member
|
154
|
+
|
155
|
+
belongs_to :position
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
```
|
162
|
+
|
163
|
+
|
164
|
+
|
65
165
|
members_controller
|
66
166
|
|
67
167
|
---
|
@@ -128,11 +228,9 @@
|
|
128
228
|
|
129
229
|
|
130
230
|
|
131
|
-
<% %w(ピッチャー キャッチャー ファースト セカンド サード ショート レフト センター ライト).each do |position| %>
|
132
|
-
|
133
|
-
|
231
|
+
<%= f.collection_check_boxes(:member_positions, Position.all, :id, :name ) do |t| %>
|
134
|
-
|
232
|
+
|
135
|
-
<%=
|
233
|
+
<%= t.label { t.check_box + t.name } %>
|
136
234
|
|
137
235
|
<% end %>
|
138
236
|
|
@@ -152,6 +250,18 @@
|
|
152
250
|
|
153
251
|
```
|
154
252
|
|
253
|
+
position内のデータ
|
254
|
+
|
255
|
+
---
|
256
|
+
|
257
|
+
```
|
258
|
+
|
259
|
+
%w[ピッチャー キャッチャー ファースト セカンド サード ショート レフト センター ライト].each { |a| Position.create(name: a) }
|
260
|
+
|
261
|
+
```
|
262
|
+
|
263
|
+
|
264
|
+
|
155
265
|
index.html.erb
|
156
266
|
|
157
267
|
-----
|
@@ -180,18 +290,4 @@
|
|
180
290
|
|
181
291
|
|
182
292
|
|
183
|
-
なのですが、実際の表示になると、
|
184
|
-
|
185
|
-
```index
|
186
|
-
|
187
|
-
氏名:田中太郎
|
188
|
-
|
189
|
-
ポジション: ["0", "{:value=>\"ピッチャー\"}", "0", "{:value=>\"キャッチャー\"}", "0", "0", "0", "0", "0", "0", "0"]
|
190
|
-
|
191
|
-
紹介文:○○○○○○○○
|
192
|
-
|
193
|
-
```
|
194
|
-
|
195
|
-
となってしまいます。
|
196
|
-
|
197
293
|
よろしくお願いいたします。
|