铁路的cancancan深入人心

时间:2017-03-17 20:27:23

标签: ruby-on-rails cancancan

在用户和ClassD之间关联深入时定义能力时遇到问题。

User < ActiveRecord::Base 
  has_many :classB
  has_many :classC, through classB
  has_many :classD, through classB
end
ClassB < ActiveRecord::Base
  belongs_to :User
  has_many :classC
  has_many :classD, through classC
end 
ClassC < ActiveRecord::Base
  belongs_to ClassC
  has_many :ClassD
end
ClassD < ACtiveRecord::Base
  belongs_to Class C
end

如何使用cancancan定义一种能力,例如:

class Ability
  include CanCan::Ability

  def initialize(user) 
    user ||= User.new      
    can :manage, ClassD do |cd|
      cd.user_id: user.id
    end

  end
end

1 个答案:

答案 0 :(得分:0)

class Ability
  include CanCan::Ability

  def initialize(user) 
    user ||= User.new      
    can :manage, ClassD do |cd|
      user.cds.exists?(cd.id)
    end

  end
end