How to handle default database values now?


#1

Since interpreting defaults in the database has been ripped out (for
PostgreSQL at least), what’s the standard way of specifying defaults
now?

Right now, I’m doing this:

model

def create
self.datetime = Time.now

super
end

I hate it. I know there are the ‘*_at/on’ automatic fields, but that’ll
entail combing through all my code changing field names. I hate that
too.

Joe


#2

Rather than “create”, setup the defaults in “intialize”. You get the
most bang for your buck this way since every new record will execute
that method regardless of how its created. I would do it like this:

DEFAULTS = {
:datetime => Time.now,
:some_other_field => ‘foo’
}

def initialize
self.attributes = DEFAULTS
end

Then you can do

Model.new
Model.create(params[:model])
Parent.models.create(params[:model])

And they all will have the proper values in it.

Joe wrote:

Since interpreting defaults in the database has been ripped out (for
PostgreSQL at least), what’s the standard way of specifying defaults
now?

Right now, I’m doing this:

model

def create
self.datetime = Time.now

super
end

I hate it. I know there are the ‘*_at/on’ automatic fields, but that’ll
entail combing through all my code changing field names. I hate that
too.

Joe