Skip to content

How to restrict data for a model attribute #234

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

You must be logged in to vote

That's an interesting example.

I would use something like that:

# assuming the model is named Product
class ProductPolicy < ApplicationPolicy
  params_filter do |params|
    params.permit(:cost, :cost_override).tap do |filtered|
      filtered.delete(:cost_override) if (filtered[:cost_override] / filtered[:cost].to_f) > 0.25 && !super_admin?
    end
  end
end

Here I delete unauthorized parameter; raising Unauthorized can be an option, too.

But I assume that both cost and cost_override present in the params. I guess, it's possible to update only the cost_override (or only the cost) param, right?

I think, using policies for form submit verification is not the right way of doing this. I woul…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@jclusso
Comment options

@palkan
Comment options

@jclusso
Comment options

Answer selected by jclusso
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