我的 Ability.rb 中有以下行
can :index, :calls, :store => { area_id: user.area_id }
在调用_controller.rb
load_and_authorize_resource :store
load_and_authorize_resource :call, :through => :store
def index
@calls = Call.accessible_by(current_ability, :index)
end
在模型 call.rb
belongs_to :store
然而,当我尝试在视图中访问 @calls 时,出现以下 SQL 错误
Mysql::Error: Unknown column 'store.area_id' in 'where clause': SELECT `calls`.* FROM `calls` INNER JOIN `stores` ON `stores`.`id` = `calls`.`store_id` WHERE `store`.`area_id` = 4 LIMIT 20 OFFSET 0
这是因为 SQL 查询应该有“WHERE stores
. area_id
= 4”。这是 CanCan 的问题还是我的设置有问题?我正在使用 CanCan 2.0,仅供参考。