I want to validate the Discount of a Sale in my Sale model.
The form for creating a Sale receives the Product data from my Warehouse model and saves it inside the Sale record:
<%= f.select(:product, Warehouse.pluck(:product).uniq, {prompt:true}, {class: 'form-control'}) %>
The Warehouse model has the discount specified for this respective product. Now I want to check if the sale.product is equal to the warehouse.product and then set the discount limit for this sale. Is that possible? Something like this:
validates_length_of :discount, maximum: Warehouse.where(:product => @sales.product).pluck(:discount), message: "Discount is to high"
Many Thanks in advance!
First of all, you should be validating numericality for numeric values.
Custom validation is unnecessary. You don't have to use constants or literals or other class-evaluation time values! Use procs! They will be called!
You pass a key as a comparison and then a proc as a value, so it is called during validation.
valudates_numericality_of :discount,
less_than_or_equal_to: proc { |model| Something.query(whatever).pluck(:wow) },
message: "is too high"
Note: you should probably also add a check for whether it's positive.
Bonus (pure fun, best not use): almost the same code with a spectacular amount of arrows
(in order: symbol <=, hashrocket =>, stabby-lambda -> () {}):
valudates_numericality_of :discount,
:<= => -> (model) { Something.query(whatever).pluck(:wow) },
:message => "is too high"
Note that 1.9 hash syntax won't work with a symbol like <= (like <=: value) so you have to stick to a hashrocket with this one, which is... another bit of pure fun.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With