前提・実現したいこと
初めて相談させて頂きます。
チャットアプリを制作したく現在進めておりますが、中間テーブルへのデータ保存でつまづいています。
今はこんなソール上で登録を行おうとしている状態ですが、ROLLBACKで返され保存できません。
最初はコントローラーの問題かと思っておりましたが、コンソールでも保存ができないので、アソシエーションの問題なのでしょうか?
ご教授のほど、よろしくお願いいたします。
発生している問題・エラーメッセージ
[12] pry(main)> Chat.create(name: "こは", user_ids: [1,2]) User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IN (1, 2) DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :nickname attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from __pry__ at (pry):12) (0.2ms) BEGIN User Exists? (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`nickname` = BINARY '・・' AND `users`.`id` != 1 LIMIT 1 DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from __pry__ at (pry):12) User Exists? (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'ggg@ooo.com' AND `users`.`id` != 1 LIMIT 1 DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :nickname attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from __pry__ at (pry):12) User Exists? (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`nickname` = BINARY '::' AND `users`.`id` != 2 LIMIT 1 DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from __pry__ at (pry):12) User Exists? (0.5ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'tatsch@gmail.com' AND `users`.`id` != 2 LIMIT 1 (0.2ms) ROLLBACK => #<Chat:0x00007fc69305e400 id: nil, name: "こは", created_at: nil, updated_at: nil>
該当のソースコード
//user.rb class User < ApplicationRecord has_many :user_chats has_many :chats, through: :user_chats end
//chat.rb class Chat < ApplicationRecord has_many :user_chats has_many :users, through: :user_chats end
//user_chat.rb class UserChat < ApplicationRecord belongs_to :user belongs_to :chat end
試したこと
[13] pry(main)> Chat.create (0.4ms) BEGIN Chat Create (0.4ms) INSERT INTO `chats` (`created_at`, `updated_at`) VALUES ('2020-12-01 07:10:15.478722', '2020-12-01 07:10:15.478722') (0.7ms) COMMIT => #<Chat:0x00007fc688622720 id: 4, name: nil, created_at: Tue, 01 Dec 2020 07:10:15 UTC +00:00, updated_at: Tue, 01 Dec 2020 07:10:15 UTC +00:00>
chatのみの保存はできます。
[14] pry(main)> UserChat.create(chat_id: 1, user_id: 1) (0.3ms) BEGIN User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 Chat Load (0.4ms) SELECT `chats`.* FROM `chats` WHERE `chats`.`id` = 1 LIMIT 1 UserChat Create (0.4ms) INSERT INTO `user_chats` (`user_id`, `chat_id`, `created_at`, `updated_at`) VALUES (1, 1, '2020-12-01 07:12:33.202836', '2020-12-01 07:12:33.202836') (1.1ms) COMMIT => #<UserChat:0x00007fc6930a6070 id: 1, user_id: 1, chat_id: 1, created_at: Tue, 01 Dec 2020 07:12:33 UTC +00:00, updated_at: Tue, 01 Dec 2020 07:12:33 UTC +00:00>
user_chatのみの保存もできます。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
ruby:2.6.5
rails:6.0.0
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/01 08:10
2020/12/01 08:12
2020/12/01 08:24