Ruby on rails 如何检查CanCanCan条件散列上的值是否在数组中

Ruby on rails 如何检查CanCanCan条件散列上的值是否在数组中,ruby-on-rails,ruby,mongodb,mongoid,cancancan,Ruby On Rails,Ruby,Mongodb,Mongoid,Cancancan,我有三个模型类:PatientBoard、medicing和Customer,它们相互关联,如下所示: 患者板 associations do has_and_belongs_to_many :customers has_many :medications end 药物治疗 associations do belongs_to :patient_board end 客户 associations do has_many :patient_

我有三个模型类:
PatientBoard
medicing
Customer
,它们相互关联,如下所示:

患者板

associations do 
      has_and_belongs_to_many :customers
      has_many :medications
end
药物治疗

associations do 
      belongs_to :patient_board
end
客户

associations do 
      has_many :patient_boards
end
由于
具有_且_属于_many
关系,我的patient _board对象如下所示:

{
  _id: ObjectId("5e160f235eb3607d57f59341"), 
  name: "test",
  customer_ids: [ObjectId("5db1e6f45eb36059a4d98e7f")]
}
can :manage, Medication, patient_board: { customers: { id: user.id } }
can [:manage], [Medication], ["#{user.id} in ( select customer_ids from patient_board where id = medications.patient_board.id ) )"] do |t|
   t.patient_board.customer_ids.include? user.
end
其中,可以访问该病人看板的所有客户的ID将位于customer_ids阵列中

问题

在尝试为用户设置权限时,我对
PatientBoard
执行了如下操作:

can :manage, PatientBoard do |pb|
    pb.customer_ids.include? user.id
end
它在很大程度上起作用。但是
create
方法仍然不起作用,所以我不得不插入以下行:

can :create, PatientBoard
现在它正常工作,但当我尝试对
药物进行同样的操作时,它没有工作:

can :manage, Medication do |m|
    m.patient_board.customer_ids.include? user.id
end
它抛出以下错误消息:

The accessible_by call cannot be used with a block 'can' definition. The SQL cannot be determined for :index Medication
所以我试着这样做:

{
  _id: ObjectId("5e160f235eb3607d57f59341"), 
  name: "test",
  customer_ids: [ObjectId("5db1e6f45eb36059a4d98e7f")]
}
can :manage, Medication, patient_board: { customers: { id: user.id } }
can [:manage], [Medication], ["#{user.id} in ( select customer_ids from patient_board where id = medications.patient_board.id ) )"] do |t|
   t.patient_board.customer_ids.include? user.
end
这样,错误信息就不再显示,但药物也不会显示。当尝试创建新药物时,它会抛出错误:

undefined method `matches?' for #<PatientBoard:0x007f663c38d0e8> Did you mean? _matches? matcher
为#定义的未定义方法“匹配项”是什么意思_比赛?匹配器
我认为这是有道理的,因为它试图比较字符串值(
user.id
)和数组值(
customer\u id

在简历中


如何使用CanCanCan的条件散列检查
user.id
是否在
customer\u id
数组中?

看起来
include?
正在
can
块内触发sql查询。尝试将客户ID转换为阵列:

can:manage,medicing do|m|
m、 患者\董事会。客户\ ID。包括哪些内容?用户id
结束

因此,在检查CanCan github上的后,我设法解决了问题

显然,我必须提供一个sql查询来匹配药物,所以我这样做:

{
  _id: ObjectId("5e160f235eb3607d57f59341"), 
  name: "test",
  customer_ids: [ObjectId("5db1e6f45eb36059a4d98e7f")]
}
can :manage, Medication, patient_board: { customers: { id: user.id } }
can [:manage], [Medication], ["#{user.id} in ( select customer_ids from patient_board where id = medications.patient_board.id ) )"] do |t|
   t.patient_board.customer_ids.include? user.
end