To maintain data integrity, I need to prevent some models from changing after certain events. For example, you cannot sell a product after it is sold.
I always implemented this in the controller, for example (pseudo-code):
def ProductsController < ApplicationController
before_filter require_product_not_sold, :only => [ :write_off ]
private
def require_product_not_sold
if @product.sold?
redirect_to @product, :error => "You can't write off a product that has been sold"
end
end
end
It just seemed to me that I, too, could do this in a model. Something like that:
def Product < ActiveRecord::Base
before_update :require_product_not_sold
private
def require_product_not_sold
if self.written_off_changed?
end
end
end
Also think that there may be several different events that require the product to not be sold.
I like the approach to the controller - you can set meaningful flash messages, and not add verification errors. But it looks like this code should be in the model (for example, if I wanted to use the model outside of my Rails application).
:)