Neil Wilson wrote:
It depends what you're trying to achieve.
For now, I wish to provide a simple, explicit, and model-centric means
of specifying default values for certain table attributes. Since I
posted above I have installed the active_record_defaults plugin and this
seems to provide most of what I desired.
Class MyTable < ActiveRecord::Base
# code dependent on active_record_defaults plugin
default :effective_from => Time.now
defaults :my_attribute => "unknown", :my_int_attribute => 21
What you don't do is rely upon defaults in the database. For me the
default calculations have to be in the model.
My practice is that default values are mostly, if not entirely,
localized to the model or constrained within the DB schema. I depart
from Rails orthodoxy on this issue and I selected my database backend
(postgresql) with care so that significant amount of data integrity
logic could be enforced by the database.
For development I want this sort of stuff close to the code so that I
can easily adjust things without having to alter the DB overmuch. In
production I anticipate that a good deal of the constraints and defaults
will be moved from the model into the DBMS. Calculated fields are a
distinct issue, as you point out, and on that matter the model indeed
may be the best (perhaps only) place for them.