-
I have a base account policy: class Accounts::BasePolicy < ApplicationPolicy
authorize :user, :account
relation_scope do |relation|
relation.where(account_id: account.account_id).order(:id)
end All my policies inherit from this parent policy so we can properly handle multi tenancy and avoid issues with records leaking across different accounts, but the problem comes when I have a specific policy that needs to add a new relation_scope class Accounts::Products::SectionPolicy < Accounts::BasePolicy
authorize :product
relation_scope do |relation|
relation.where(product_id: product.id).order(:id)
end authorize is inherited as expected(section policy will require user, account and product) But its overwriting relation_scope, i would like to have a "default_scope" for inherited policies, how can I properly achieve that where there are no room for human errors when someone overwrites the relation_scope and forgets to add the account_id filter? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You mean something like scopes chaining? That is, the Then you can use class Accounts::Products::SectionPolicy < Accounts::BasePolicy
authorize :product
relation_scope do |relation|
super(relation).where(product_id: product.id).order(:id)
end
end Added an example to the docs: d8c76db |
Beta Was this translation helpful? Give feedback.
-
Exactly what I was looking for <3 |
Beta Was this translation helpful? Give feedback.
You mean something like scopes chaining? That is, the
relation
passed to the child class block should be transformed by the default block?Then you can use
super
:Added an example to the docs: d8c76db