次のように、2 つのモデル間に多対多の関係があります。
#users.rb
has_many :users_to_roles
has_many :roles, through: :users_to_roles
#users_to_roles.rb
belongs_to :user
belongs_to :role
#roles.rb
has_many :users_to_roles
has_many :users, through: :users_to_roles
「このロールにいる」ユーザーがいる場合、ロールの削除を無効にしたい。ここで、作業を行うべき 2 つのオプションを見つけました。
:restrict_with_exception は、関連付けられたレコードがある場合に例外を発生させます :restrict_with_error は、関連付けられたオブジェクトがある場合に所有者にエラーを追加します
しかし、これの構文とそれがどのように機能するかの例はありません。
これを有効にするのを手伝ってもらえますか:
#roles.rb
has_many :users_to_roles
has_many :users, through: :users_to_roles, dependent: restrict_with_exception