CanCan双重嵌套资源许可问题

问题描述

| 我在定义两次嵌套资源的权限时遇到一些问题。我有用户>公司>订单... 我的用户通过协议有很多公司。 每个公司都有很多订单,每个订单都属于一个公司。 我的abilities.rb文件包含以下内容
elsif user.role? :customer_admin
      can [:read,:update],User,:id => user.id
      can [:read,Company,:id => user.id 
      can :read,:users => { :id => user.id }
      can :read,Order,:user => { :id => user.id }
 end
在我的订单控制器中,我有这个:
 load_and_authorize_resource :company
  load_and_authorize_resource :order,:through => :company
问题是我似乎无法以customer_admin身份查看订单 希望您能提供帮助,再次感谢。 ----编辑---- user.rb
 has_and_belongs_to_many :roles
  has_many :agreements
  has_many :companies,:through => :agreements
company.rb
  has_many :agreements
  has_many :users,:through => :agreements
  has_many :orders
  accepts_nested_attributes_for :orders
order.rb
  belongs_to :company
  has_many :comments
  has_many :tasks
  has_many :requirements
  has_many :services,:through => :requirements
  has_many :servicelevelagreements
  has_many :slas,:through => :servicelevelagreements
Agreement.rb
  belongs_to :user
  belongs_to :company
希望对您有所帮助!!     

解决方法

        您的订单中是否有
user_id
,它定义了管理员用户? 您似乎想在
has_many :through
关联中使用它。如果是这种情况,那么我建议尝试通过如下定义进行访问:
can :read,Order,:company => { :user_id => user.id }
由于ѭ9支持嵌套关联。 更新资料 我的设置假设您的模型如下所示:
#order.rb

belongs_to :company

#company.rb

belongs_to :user
has_many :orders
并且您的公司应包含一个名为“ 6”的字段,这是分配的用户的ID。 有关更多信息,请参见Wiki。 https://github.com/ryanb/cancan/wiki/Nested-Resources 更新2 问题是您的
company
has_many :users,:through=>:agreements
这涉及以下定义:
can :read,:company => { :users => { :id => user.id } }