简体   繁体   中英

Error when deleting user in active_admin

I get the following error when I attempt to delete a user :

undefined method `handle_dependency' for #<ActiveRecord::Associations::HasAndBelongsToManyAssociation:0x007fa889b27328>)

My User model is:

class User < ActiveRecord::Base
  rolify
  attr_accessible :user_attributes
  attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :role_ids
  has_one :role, :dependent => :destroy

  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

  validates_presence_of :username, :email, :password, :case_sensitive => true
  validates_uniqueness_of :email, :case_sensitive => false
  validates_uniqueness_of :username, :case_sensitive => false

  before_create :assign_role

  def assign_role
    self.add_role :user if self.roles.first.nil?
  end

  after_create { |admin| admin.send_reset_password_instructions }

end

My Role model is:

class Role < ActiveRecord::Base
  has_many :users, :through => :users_roles, :dependent => :destroy
  belongs_to :resource, :polymorphic => true
  attr_accessible :role_attributes
  resourcify
  scopify
end

Am I doing something wrong? The handle_dependency method doesn't seem to exist. Will greatly appreciate any help.

Thanks.

I can't explain that specific error, but it looks like there is some confusion about rolify's macro methods do. Here is some background:

rolify establishes a has_and_belongs_to_many between the User and Role models. So the has_one call in your User model is unnecessary and might be causing problems.

resourcify is used to indicate that users can have one or more roles with respect to a given class or instances of that class. Users don't have roles with respect to Roles, they have roles with respect to Posts or Articles or whatever. So the resourcify call in your Role model is very likely causing a problem. You need to call resourcify in the class for the models to which you are trying to restrict access.

I've investigated this as I'm trying to rolify and resourcify a model other than User or Role . I don't have a clear answer but I can provide what I've found. If nothing else, make sure you have the latest version of rolify ("bundle update rolify").

I've answered a similar issue.

https://stackoverflow.com/a/23003808/1011746

Unfortunately, I've been unable to figure out how the :roles association is being overwritten in your code. It's possible that it's something other than :roles that is being overwritten but I haven't located it.

This isn't much of an answer but I hope it points in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM