我正在尝试定义用户基于关联模型上的列访问某些内容的能力(例如can :read, Step, 'steppable' => {published: true}
),问题是它是一个多态关联,因此它无法找到可步进表,因为它不存在。
我有步骤,每个步骤都有一个步骤(讲座、测验或其他动作)。我需要一个可以工作的 activerecord 查询。我试过了:
Step.includes(:steppable).where('steppable' => {published: true})
和
Step.joins(:steppable).where('steppable' => {published: true})
但两者都导致ActiveRecord::EagerLoadPolymorphicError: Can not eagerly load the polymorphic association :steppable
模型如下所示:
class Step < ActiveRecord::Base
...
belongs_to :steppable, polymorphic: true, dependent: :destroy
...
end
和
class Lecture
...
has_one :step, as: :steppable, dependent: :destroy
...
end
注意:我想对关联的模型不可知,为了使它能够使用 CanCan 获取记录,它必须使用数据库列来完成(参见github.com/ryanb/cancan/wiki/defining-abilities )