質問編集履歴

3

userモデルの追記

2021/03/23 03:19

投稿

kawakun-----
kawakun-----

スコア13

test CHANGED
File without changes
test CHANGED
@@ -352,6 +352,108 @@
352
352
 
353
353
  ```
354
354
 
355
+
356
+
357
+ バリデーションが原因の可能性が高いということで
358
+
359
+
360
+
361
+ userモデル
362
+
363
+ ```
364
+
365
+ class User < ApplicationRecord
366
+
367
+
368
+
369
+ devise :database_authenticatable, :registerable,
370
+
371
+ :recoverable, :rememberable, :validatable
372
+
373
+ with_options presence: true do
374
+
375
+ validates :name
376
+
377
+ validates :birth_day
378
+
379
+ validates :gender
380
+
381
+ validates :hobby
382
+
383
+ validates :self_introduction, length: { maximum: 200 }
384
+
385
+ validates :image
386
+
387
+ end
388
+
389
+
390
+
391
+ enum gender: { man: 0, woman: 1}
392
+
393
+
394
+
395
+ has_one_attached :image
396
+
397
+
398
+
399
+ has_many :relationships
400
+
401
+ has_many :followings, through: :relationships, source: :follow
402
+
403
+ has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id'
404
+
405
+ has_many :followers, through: :reverse_of_relationships, source: :user
406
+
407
+
408
+
409
+ def follow(other_user)
410
+
411
+ unless self == other_user
412
+
413
+ self.relationships.find_or_create_by(follow_id: other_user.id)
414
+
415
+ end
416
+
417
+ end
418
+
419
+
420
+
421
+ def unfollow(other_user)
422
+
423
+ relationship = self.relationships.find_by(follow_id: other_user.id)
424
+
425
+ relationship.destroy if relationship
426
+
427
+ end
428
+
429
+
430
+
431
+ def following?(other_user)
432
+
433
+ self.followings.include?(other_user)
434
+
435
+ end
436
+
437
+
438
+
439
+ has_many :room_users
440
+
441
+ has_many :rooms, through: :room_users
442
+
443
+ has_many :messages
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+ end
452
+
453
+
454
+
455
+ ```
456
+
355
457
  なぜ中間テーブルに保存できないのかわかりません。。。
356
458
 
357
459
 

2

@room.errors確認してみると

2021/03/23 03:18

投稿

kawakun-----
kawakun-----

スコア13

test CHANGED
File without changes
test CHANGED
@@ -264,12 +264,12 @@
264
264
 
265
265
 
266
266
 
267
- ```
268
-
269
267
  コントローラーにuser_idsで記載してみると、
270
268
 
271
269
  ターミナルに下記のようなエラーが出ます。。。
272
270
 
271
+ ```
272
+
273
273
 
274
274
 
275
275
  (0.4ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
@@ -316,11 +316,41 @@
316
316
 
317
317
 
318
318
 
319
- となるので、
320
-
321
- user_idsで呼び出せているので、
322
-
323
- 再度、user_idsでも実行ましたが、
319
+ @room.errors確認てみると
320
+
321
+ ```
322
+
323
+ 7: def create
324
+
325
+ 8: @room = Room.new(room_params)
326
+
327
+ 9: if @room.save
328
+
329
+ 10: redirect_to room_messages_path(@room)
330
+
331
+ 11: else
332
+
333
+ 12: render :new
334
+
335
+ => 13: binding.pry
336
+
337
+ 14: end
338
+
339
+ 15: end
340
+
341
+
342
+
343
+ [1] pry(#<RoomsController>)> @room.errors
344
+
345
+ => #<ActiveModel::Errors:0x00007fceba1fc148
346
+
347
+ @base=#<Room:0x00007fceb9f546e8 id: nil, name: "aa", created_at: nil, updated_at: nil>,
348
+
349
+ @details={:users=>[{:error=>:invalid}, {:error=>:invalid}]},
350
+
351
+ @messages={:users=>["is invalid"], :Neme=>[], :name=>[]}>
352
+
353
+ ```
324
354
 
325
355
  なぜ中間テーブルに保存できないのかわかりません。。。
326
356
 

1

user_idsで実行した場合

2021/03/23 03:13

投稿

kawakun-----
kawakun-----

スコア13

test CHANGED
File without changes
test CHANGED
@@ -264,6 +264,58 @@
264
264
 
265
265
 
266
266
 
267
+ ```
268
+
269
+ コントローラーにuser_idsで記載してみると、
270
+
271
+ ターミナルに下記のようなエラーが出ます。。。
272
+
273
+
274
+
275
+ (0.4ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
276
+
277
+ Processing by RoomsController#create as HTML
278
+
279
+ Parameters: {"authenticity_token"=>"al727tBbTypIjy3vZRnlciMGVnQ3lfvesTH94yYqIH3wTrl9DlvcSfHC8vY9gJtqwCrvJ4ddKQddtKxGln9t2Q==", "room"=>{"name"=>"aa", "user_ids"=>["2", "6"]}, "commit"=>"Create Room"}
280
+
281
+ User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 6 ORDER BY `users`.`id` ASC LIMIT 1
282
+
283
+ User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IN (2, 6)
284
+
285
+ ↳ app/controllers/rooms_controller.rb:8:in `create'
286
+
287
+ (0.2ms) BEGIN
288
+
289
+ ↳ app/controllers/rooms_controller.rb:9:in `create'
290
+
291
+ ActiveStorage::Attachment Load (1.2ms) SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 2 AND `active_storage_attachments`.`record_type` = 'User' AND `active_storage_attachments`.`name` = 'image' LIMIT 1
292
+
293
+ ↳ app/controllers/rooms_controller.rb:9:in `create'
294
+
295
+ ActiveStorage::Attachment Load (0.6ms) SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 6 AND `active_storage_attachments`.`record_type` = 'User' AND `active_storage_attachments`.`name` = 'image' LIMIT 1
296
+
297
+ ↳ app/controllers/rooms_controller.rb:9:in `create'
298
+
299
+ (0.1ms) ROLLBACK
300
+
301
+ ↳ app/controllers/rooms_controller.rb:9:in `create'
302
+
303
+ Rendering rooms/new.html.erb within layouts/application
304
+
305
+ User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` != 6
306
+
307
+ ↳ app/views/rooms/new.html.erb:21
308
+
309
+ Rendered rooms/new.html.erb within layouts/application (Duration: 1.9ms | Allocations: 1406)
310
+
311
+ [Webpacker] Everything's up-to-date. Nothing to do
312
+
313
+ Completed 200 OK in 86ms (Views: 14.0ms | ActiveRecord: 10.9ms | Allocations: 53947)
314
+
315
+ ```
316
+
317
+
318
+
267
319
  となるので、
268
320
 
269
321
  user_idsで呼び出せているので、