Changing business rule verification over time in Rails

Several times I've been in a situation where we have a model with business process validation, for example:

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 be validated, and any logic where i call order.save (), it starts crashing (sometimes frustratingly quiet). I've encountered this many times in a fairly large Rails delivery application, and haven't found a good solution yet. Some ideas:

  • Make sure pending "orders" will not be processed when the code change is changed.
  • Add :if => Proc.new { |o| o.created_at.nil? or o.created_at > date_new_validation_is_effective }

    to the new check, but it quickly gets cumbersome.
  • "Validate" business rules somewhere else, such as controllers, where user input is entered rather than as a model validation. But this violates the Fat Model / Skinny Controller principle, which has many supporters in Rails.

Is there another approach for integrating this logic or keeping a strategy like # 2 manageable in the long run?

+3


source to share


2 answers


You can configure this business logic validation to run only :on => :create

. I am assuming that you do not often edit / update the order total.



This will apply to all future orders without affecting the validity of existing models in the system.

+2


source


You can add version to order record and version check.



+1


source







All Articles