1

我的 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,仅供参考。

4

2 回答 2

0

尝试改变以下能力:

can :index, Call, :store => { area_id: user.area_id }
于 2014-11-26T19:37:36.227 回答
0

如果您限制用户可以看到的商店

  if !user.area_id.nil? and user.area_id != 0 then
    user_stores = Store.where(:area_id => user.area_id)
  end

然后你可以在条件中传递一个数组

  can [:read, :index], :stores, :id => user_stores
  can [:read, :index, :create, :update], [:calls, :change_requests], :store_id => user_stores

这按要求工作。

于 2014-11-27T10:02:57.267 回答