gpt4 book ai didi

ruby-on-rails - 在 Cancancan 中定义组合复杂能力规则

转载 作者:太空宇宙 更新时间:2023-11-03 16:47:51 25 4
gpt4 key购买 nike

我有一个我似乎无法定义的复杂规则,用户可以阅读他们自己的信件,或者如果他们有权限,他们可以阅读其他人的信件,这些信件没有链接到任何类型(如果他们是那么类型控制谁能看到它)。

class Letter < ActiveRecord::Base
belongs_to :letter_template
has_one :letter_template_type, through: :letter_template
end

class LetterTemplateType < ActiveRecord::Base
has_and_belongs_to_many :types
end

我试过这个,认为它会覆盖它,但查看生成的 sql 它执行“或(非字母匹配规则)”而不是“和(非字母匹配规则)”。

can :read, Letter
cannot :read, Letter, letter_template_type: { types: { active: [true, false] } } # the active [true, false] is just to find any type

我尝试的另一种方法是使用 block /作用域,但它会提示“CanCan::错误: 无法将 Active Record 作用域与其他条件合并。而是使用哈希或 SQL 来实现读取字母的能力。”

can :read, Letter, Letter.without_type do |l|
l.letter_template_type.nil? || l.letter_template_type.types.empty?
end

谁能帮我定义一个针对这种情况的规则?

最佳答案

这样使用,

 can :read, Letter, [] do |l|
l.letter_template_type.nil? || l.letter_template_type.types.empty?
end

你还必须在上面的 block 中添加a user can read their own letters条件,可能是l.user == your_ability_user

关于ruby-on-rails - 在 Cancancan 中定义组合复杂能力规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29485601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com