【问题标题】:Rail's cancancan deep has_many throughRail 的 cancancan 深度 has_many 通过
【发布时间】:2017-03-17 20:27:23
【问题描述】:

当用户和 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

【问题讨论】:

    标签: ruby-on-rails cancancan


    【解决方案1】:
    class Ability
      include CanCan::Ability
    
      def initialize(user) 
        user ||= User.new      
        can :manage, ClassD do |cd|
          user.cds.exists?(cd.id)
        end
    
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-18
      相关资源
      最近更新 更多