我正在使用CanCan(Can)和ActiveAdmin。但是,我正在努力让CanCan(Can)在“has_many through”关系的索引上正常工作。
基本上我的发票模型看起来像这样
class Invoice < ActiveRecord::Base
belongs_to :order
belongs_to :country
belongs_to :currency
has_one :user, :through => :order
(…)
我的订单模型就是这样
class Order < ActiveRecord::Base
belongs_to :user
belongs_to :product
has_many :invoices
我的用户模型就像这样
class User < ActiveRecord::Base
has_many :orders
has_many :invoices, :through => :orders
我的能力定义如下
可以:读取,发票,:用户=&gt; adminuser.user
这适用于个人发票。因此,正确的用户可以看到此URL:3000 / admin / invoices / 1,而其他用户将收到未经授权的错误。
然而,在索引列表上,它完全向南。 :3000 / admin / invoices /返回错误消息
Mysql2 ::错误:未知列&#39; invoices.user_id&#39;在&#39; where子句&#39;: SELECT COUNT(count_column)FROM(SELECT 1 AS count_column FROM
invoices
WHEREinvoices
。user_id
= 2 LIMIT 30 OFFSET 0) subquery_for_count
显然这是完全错误的,因为CanCan(Can)正在查看错误的表格。如何设置ActiveAdmin和CanCan(Can)对索引上的此关系使用“直通”查找?我试过添加
def authorize_access!
load_and_authorize_resource :through => :order
end
到“ActiveAdmin.register Invoice”的控制器,但它没有任何区别。
任何建议都将不胜感激!
答案 0 :(得分:1)
试试这个:can :read, Invoice, :user => { :id => adminuser.user.id }