チーム情報を持つTeamモデル、ユーザー情報を持つUserモデルを下記のように作成しました。
-
Teamsテーブル
PK id String team_name String password_digest String FK admin_id Integer -
Usersテーブル
PK id Integer FK team_id String user_name String email String password_digest String
user.rb
1class User < AppilicationRecord 2 belongs_to :team_id, class_name 'Team', foreign_key: 'id', optional: true 3 has_one :admin, class_name: 'Team', foreign_key: 'user_id' 4end
team.rb
1class Team < AppilicationRecord 2 belongs_to :admin_id, class_name 'User', foreign_key: 'id', optional: true 3 has_many :user_id, class_name: 'User', foreign_key: 'team_id' 4end
その後、railsのコンソールから、それぞれのインスタンスを作成し検証しましたが、下記のようにエラーが出てしまいました。どなたか原因をご教授いただけると幸いです。
$ user1 = User.new(team_id: "team1", user_name: "テスト", email: "test@test.com", password_digest: "pass") `rails_on_type_mismatch!` Team(#19220) expected, got "team1" which is an instance of String(#1920) (ActiveRecord::AssociationTypeMismatch)

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/21 14:19
2022/05/21 22:54