Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Permissions Rails 4-限制对共享对象的访问的最佳实践是什么_Permissions_Ruby On Rails 4_Cancan - Fatal编程技术网

Permissions Rails 4-限制对共享对象的访问的最佳实践是什么

Permissions Rails 4-限制对共享对象的访问的最佳实践是什么,permissions,ruby-on-rails-4,cancan,Permissions,Ruby On Rails 4,Cancan,用户作为买家签订了许多合同 用户作为卖家有很多合同 我知道我可以使用CanCan来限制用户拥有的东西。但在这种情况下,我有一个既有买方又有卖方的合同。我希望用户能够查看/阅读他们作为买方或卖方的所有合同 我试着设置一个范围,并将其与CanCan一起使用,但这似乎不起作用 我把我的能力设置成这样 can :read, Contract.parties(user.id) 我的范围定义为 scope :parties, lambda { |user_id| where("seller_id = ?

用户作为买家签订了许多合同 用户作为卖家有很多合同

我知道我可以使用CanCan来限制用户拥有的东西。但在这种情况下,我有一个既有买方又有卖方的合同。我希望用户能够查看/阅读他们作为买方或卖方的所有合同

我试着设置一个范围,并将其与CanCan一起使用,但这似乎不起作用

我把我的能力设置成这样

can :read, Contract.parties(user.id)
我的范围定义为

scope :parties, lambda { |user_id| where("seller_id = ? OR buyer_id = ?", user_id, user_id) }
我也试着以不同的方式设置这些能力

can :read, Contract, buyer_id: user.id
can :read, Contract, seller_id: user.id

但是上面的内容似乎有冲突并抛出错误

尝试使用具有CanCan能力的块:

can :read, Contract do |c|
  (c.buyer_id == user.id) || (c.seller_id == user.id)
end

我不知道你能做到。我想试一试,但我已经换到了保护宝石。我相信你,并把你的答案记为正确。我也会去看看保护宝石,从没听说过。