Several times I've been in a situation where we have a model with a business-driven validation such as:
class Order < ActiveRecord::Base
validates_numericality_of :total, :greater_than => 5.0
# Some more logic
end
At some point, the boss man decides that the new minimum order should be $10, so we update the validation to 10. However, this means that any existing orders with values between $5 and $10 will no longer validate, and any logic where I call order.save() will begin to fail (sometimes unpleasantly silently). I've run into this many times in a largish shipping Rails app, and haven't found a good solution yet. Some ideas:
:if => Proc.new { |o| o.created_at.nil? or o.created_at > date_new_validation_is_effective } to the new validation, but certain this quickly becomes unwieldyIs there a different approach for integrating this logic, or keeping a strategy like #2 manageable in the long run?
You could set this business logic validation to only run :on => :create. I'm assuming that you don't often edit/update the total of an order.
That would put it into effect for all orders going forward, while not affecting the validity of existing models in the system.
You could add a version to the order record and version specific validations.
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