【发布时间】:2013-09-19 14:03:06
【问题描述】:
我正在尝试根据关联模型上的列(例如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 )
【问题讨论】:
-
您是否尝试过
Step.includes(:steppable).all,只是为了检查一下?你的模型是什么样的?我们需要更多信息... -
是的,Step.includes(:steppable).all 有效,但 Step.includes(:steppable).where('steppable' => {published: true}).all 导致相同的@987654329 @ 错误。我用模型信息更新了问题。
标签: ruby-on-rails activerecord cancan