Ruby on rails 如何在RubyonRails中使用dependent?

Ruby on rails 如何在RubyonRails中使用dependent?,ruby-on-rails,ruby,ruby-on-rails-4,has-many,Ruby On Rails,Ruby,Ruby On Rails 4,Has Many,用户\u controller.rb: class User < ActiveRecord::Base has_many :posts, dependent: :restrict_with_exception end 为什么它没有捕获异常?因为没有关联的post记录什么????????????????????? class UsersController < ApplicationController ... def destroy @user.destroy

用户\u controller.rb:

class User < ActiveRecord::Base
  has_many :posts, dependent: :restrict_with_exception
end

为什么它没有捕获异常?

因为没有关联的post记录什么?????????????????????
class UsersController < ApplicationController
...
  def destroy
    @user.destroy   
    flash[:danger] = "Success"
    redirect_to users_path
  rescue => e
    flash[:danger] = "Error"
    redirect_to @user
  end
...
end