【发布时间】:2013-11-01 04:15:15
【问题描述】:
我想使用 Protector 来控制未经授权的用户可以在我的模型上看到的字段,但我无法使用它来隐藏关联。
我有一个项目模型和一个后期模型。它们部分看起来像这样。
class Project < ActiveRecord::Base
has_many :posts
protect do
can :read, %w(title)
cannot :read, %w(posts)
end
end
class Post < ActiveRecord::Base
belongs_to :project
protect do
cannot :read
end
end
现在假设我创建了一些看起来像这样的记录
Project.create(title: 'project one')
User.create(email: 'example@user.com')
Post.create(project_id: Project.first.id, title: 'post one')
Post.create(project_id: Project.first.id, title: 'post two')
当我请求Project.first.restrict!(User.first).posts 时,我得到一个非空的 ActiveRecord 关系。我无法访问关系中对象的标题,但我可以看到它们的 ID 以及有多少帖子。我宁愿能够限制对 Project 对象的访问,以便根本不返回任何帖子。使用 Protector 是否可以做到这一点,还是我应该寻找其他解决方案?
【问题讨论】:
标签: ruby-on-rails ruby activerecord protector