Pundit:根据加入者获得访问权限的正确方法 table
Pundit: Proper way to gain access based on joiner table
Pundit 有点陌生。我有 3 个模型和一个木工 table; User
、Customer
和 Route
模型,以及允许用户拥有路线的联接器 table。客户属于特定路线。
如果 she/he 在 routes_users
加入者 table 中有路线,我只想授权用户会见客户。
我发现自己有很多方法可以做到这一点。但是,实现它的最佳方法是什么?
customer_policy.rb
def show?
if user.admin? || user.sales_manager?
true
else
user.routes.map(&:id).include? record.route_id
end
end
使用 .map 不是一个好主意,因为它会填充数据库中的所有路由以便在本地搜索它们,最好是使用 .where
和不是 .map
如下:
def show?
if user.admin? || user.sales_manager?
true
else
user.routes.where(record.route_id).count > 0
end
end
Pundit 有点陌生。我有 3 个模型和一个木工 table; User
、Customer
和 Route
模型,以及允许用户拥有路线的联接器 table。客户属于特定路线。
如果 she/he 在 routes_users
加入者 table 中有路线,我只想授权用户会见客户。
我发现自己有很多方法可以做到这一点。但是,实现它的最佳方法是什么?
customer_policy.rb
def show?
if user.admin? || user.sales_manager?
true
else
user.routes.map(&:id).include? record.route_id
end
end
使用 .map 不是一个好主意,因为它会填充数据库中的所有路由以便在本地搜索它们,最好是使用 .where
和不是 .map
如下:
def show?
if user.admin? || user.sales_manager?
true
else
user.routes.where(record.route_id).count > 0
end
end