Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 如果存在关联,如何禁止删除_Ruby On Rails 3_Activerecord_Dependencies_Model Associations - Fatal编程技术网

Ruby on rails 3 如果存在关联,如何禁止删除

Ruby on rails 3 如果存在关联,如何禁止删除,ruby-on-rails-3,activerecord,dependencies,model-associations,Ruby On Rails 3,Activerecord,Dependencies,Model Associations,我认为两个模型之间存在如下多对多关系: #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 如果有“在此角色中”的用户,我想禁用删除

我认为两个模型之间存在如下多对多关系:

#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
如果有“在此角色中”的用户,我想禁用删除角色。我找到了两个应该做这项工作的人:

:restrict_with_exception导致在存在异常时引发异常 任何关联的记录:使用\u错误限制\u会导致错误被删除 如果存在任何关联对象,则添加到所有者

但是没有关于这个语法和它应该如何工作的例子

您是否可以帮助使此有效:

#roles.rb
has_many :users_to_roles
has_many :users, through: :users_to_roles, dependent: restrict_with_exception

这样的操作可以很容易地使用。在我的例子中,我在模型中添加了以下方法:

# callbacks
before_destroy :check_for_users_in_this_role

def check_for_users_in_this_role
  status = true
  if self.security_users.count > 0
    self.errors[:deletion_status] = 'Cannot delete security role with active users in it.'
    status = false
  else
    self.errors[:deletion_status] = 'OK.'
  end
  status
end

或者,您可以在控制器中拯救异常。在本例中,联系人可能拥有自己的利益,即

  class Interest < ActiveRecord::Base
    belongs_to :contact
  end

  class Contact < ActiveRecord::Base
    has_many :interests, :dependent => :restrict
   end

flash消息将显示在呼叫页面中。

正确的rails方法是执行以下操作:

users.rb:

has_many :users_to_roles, dependant: :destroy # don't keep the join table entry if the user is gone
has_many :roles, through: :users_to_roles
确保联接没有冗余条目(其中任一列为null或孤立)

用户到角色.rb:

belongs_to :user
belongs_to :role

# add validations presence of both user and role
# in both model and database.
另外,从rails 4.2中,您可以在迁移中添加
forigen_key:true
,以

现在在你的角色中(我假设你给你的模特起名很奇怪,并且在问题中打了个错字),你添加了以下内容:

role.rb:

has_many :users_to_roles, dependant: :restrict_with_error
has_many :users, through: :users_to_roles

我的课程是这样做的:

app/models/guest\u chat\u token.rb

class GuestChatToken < ApplicationRecord

  has_many :chat_messages, as: :sendable, dependent: :restrict_with_exception

end
class Admin::ApplicationController < ApplicationController
....
    rescue_from ActiveRecord::DeleteRestrictionError do |exception|
            redirect_to :back, notice:
            "Be aware: #{exception.message}."
    end
end
class GuestChatToken
app/controllers/admin/application\u controller.rb

class GuestChatToken < ApplicationRecord

  has_many :chat_messages, as: :sendable, dependent: :restrict_with_exception

end
class Admin::ApplicationController < ApplicationController
....
    rescue_from ActiveRecord::DeleteRestrictionError do |exception|
            redirect_to :back, notice:
            "Be aware: #{exception.message}."
    end
end
class Admin::ApplicationController
您在该区域的视图是什么样的?如果不希望用户删除某些内容,可以使用Ruby在指定条件下禁用delete按钮/链接。我认为这比允许删除操作启动,然后在不满足条件时生成错误要好得多。@mbrach我已经这样做了。你认为这够了吗?我是rails新手,但我觉得这种保护不够好——毕竟,如果有人成功删除角色,应用程序将停止正常工作。啊,这是个好问题。我认为用户可以构建一个路由,当您不打算删除它时,它将是一个删除。您可以执行如下所述的操作: