Home

小鱼驿站

29 Jun 2014

How to use cancan in rails model?

There’s a wiki entry at github for this: http://wiki.github.com/ryanb/cancan/ability-for-other-users

You need to change your model:

class User < ActiveRecord::Base  
  def ability
    @ability ||= Ability.new(self)  
  end  

  delegate :can?, :cannot?, :to => :ability
end

Then you can check abilities like this:

user.can?(:update,@article)

Til next time,
small_fish__ at 14:38

scribble