Skip to content

Policy inheritance for relation_scope #216

Answered by palkan
danielnc asked this question in Q&A
Discussion options

You must be logged in to vote

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?

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:

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

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by danielnc
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants