Search code examples
ruby-on-railsruby-on-rails-3cancan

How can Cancan ability handle two user models


I working permision with cancan gem.

How can I get "CANCAN" to manage permission for two models?

thanks

==================================

I can do this ????

class Ability

  include CanCan::Ability

  def initialize(user)
   # code
  end

  def initialize(accounts)
     #code  
  end
end

Solution

  • You need to specify what models certain users can manipulate and perform actions on in your ability.rb file.

    class Ability
      include CanCan::Ability
    
      # usual setup
      def initialize(user)
        user ||= User.new
    
        if user.has_role? :admin
          can :manage, :all
        end
    
        if user.has_role? :less_important_role
          can [:read, :update], Model1
          can :manage, Model2
        end
      end
    end
    

    Checkout the docs to read more about defining abilities.