質問編集履歴

2

前提・実現したいことを書き直しました。

2020/03/28 04:15

投稿

pancho
pancho

スコア12

test CHANGED
File without changes
test CHANGED
@@ -1,11 +1,21 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
+ 社内ToDoアプリを作成中です
4
+
5
+ user(社員)がboard(情報を載せる所)を作った後
6
+
7
+ そのboardを共有できる人を選択するのに
8
+
3
9
  チェックボックスを使った、多対多の保存を行おうとしたのですが
4
10
 
5
11
  下記エラーがでて進めなくなりました。
6
12
 
7
13
 
8
14
 
15
+ 中間テーブルとして、roomを作っています。
16
+
17
+
18
+
9
19
  ### 発生している問題・エラーメッセージ
10
20
 
11
21
  ![イメージ説明](180eff437f0eff6a827895e0c3b9f431.png)

1

ソースコードにuser,board,room(中間テーブル)のmigrateとModelを載せました。

2020/03/28 04:15

投稿

pancho
pancho

スコア12

test CHANGED
File without changes
test CHANGED
@@ -84,6 +84,166 @@
84
84
 
85
85
  ```
86
86
 
87
+ ### 追加情報
88
+
89
+ user
90
+
91
+ ```
92
+
93
+ class CreateUsers < ActiveRecord::Migration[5.2]
94
+
95
+ def change
96
+
97
+ create_table :users do |t|
98
+
99
+ t.string :name
100
+
101
+ t.string :email
102
+
103
+ t.string :password_digest
104
+
105
+ t.references :department, foreign_key: true
106
+
107
+ t.boolean :admin, default: false
108
+
109
+
110
+
111
+ t.timestamps
112
+
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+
120
+
121
+ ```
122
+
123
+ ```
124
+
125
+ class User < ApplicationRecord
126
+
127
+ before_save { self.email.downcase! }
128
+
129
+ validates :name, presence: true, length: { maximum: 10 }
130
+
131
+ validates :email, presence: true, length: { maximum: 255 },
132
+
133
+ format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i },
134
+
135
+ uniqueness: { case_sensitive: false }
136
+
137
+ has_secure_password
138
+
139
+
140
+
141
+ belongs_to :department
142
+
143
+ has_many :boards, through: :rooms
144
+
145
+ has_many :rooms
146
+
147
+ has_many :logs, dependent: :destroy
148
+
149
+ end
150
+
151
+ ```
152
+
153
+
154
+
155
+
156
+
157
+ board
158
+
159
+ ```
160
+
161
+ class CreateBoards < ActiveRecord::Migration[5.2]
162
+
163
+ def change
164
+
165
+ create_table :boards do |t|
166
+
167
+ t.string :name, null: false
168
+
169
+ t.references :user, foreign_key: true
170
+
171
+ t.string :release
172
+
173
+
174
+
175
+ t.timestamps
176
+
177
+ end
178
+
179
+ end
180
+
181
+ end
182
+
183
+
184
+
185
+ ```
186
+
187
+ ```
188
+
189
+ class Board < ApplicationRecord
190
+
191
+ has_many :users, through: :rooms
192
+
193
+ has_many :rooms
194
+
195
+ has_many :lists, dependent: :destroy
196
+
197
+ end
198
+
199
+
200
+
201
+ ```
202
+
203
+
204
+
205
+ room(中間テーブル)
206
+
207
+ ```
208
+
209
+ class CreateRooms < ActiveRecord::Migration[5.2]
210
+
211
+ def change
212
+
213
+ create_table :rooms do |t|
214
+
215
+ t.references :user, foreign_key: true
216
+
217
+ t.references :board, foreign_key: true
218
+
219
+
220
+
221
+ t.timestamps
222
+
223
+ t.index [:user_id, :board_id], unique: true
224
+
225
+ end
226
+
227
+ end
228
+
229
+ end
230
+
231
+
232
+
233
+ ```
234
+
235
+ ```
236
+
237
+ class Room < ApplicationRecord
238
+
239
+ belongs_to :user
240
+
241
+ belongs_to :board
242
+
243
+ end
244
+
245
+ ```
246
+
87
247
 
88
248
 
89
249
  ### 試したこと