1
Rubyをバージョン2.3から2.4にアップデートし、Rails 5を使用してmysqlデータベースで新しいアプリケーションを作成しています。has_and_belongs_to_Rails 5の新しいアプリケーションの作成に関係のない多くの関係
rails new myapp1 -d mysql
そして私は接続モデルそして
rails g migration CreateRolesUsers users:references roles:references
に従うように、私はモデル上の関係を定義するためのマイグレーションを作成した後2つのユーザー様モデルとロール
rails g model Users username:string
rails g model Roles rolename:string
を作成します
class User < ApplicationRecord
has_and_belongs_to_many :roles
end
class Role < ApplicationRecord
has_and_belongs_to_many :users
end
は最後に、私はレールのコンソールを開き、上記の最後の行が次のエラーを生成し、次のコマンド
@user = User.new(:username => "Nifras")
@role = Role.new(:rolename => "admin")
@role.save
@user.roles << @roles
を実行します。
ActiveModel::UnknownAttributeError: unknown attribute 'user_id' for User::HABTM_Roles.
しかし、私の関係テーブルは、user_idは、それが自己を持っています。どこが間違っていて、どのようにこの問題を克服するのですか?
ありがとうございます。