Default column values and creating new rows with ActiveRecor

Hello,
I have a table that looks kind of like this:
CREATE TABLE blahs (
id SERIAL PRIMARY KEY,

modified_at DATETIME NOT NULL DEFAULT now()
)

When I try to create a new row using ActiveRecord, it bombs saying I’m
not allowed to set modified_at to NULL.

new = Blah.new
new.some_field = some_value
new.save! # ERROR: null value in column “modified_at” violates not-
null constraint (ActiveRecord::StatementInvalid)

Is there any way to let ActiveRecord use the default value set at the
database level?

I’m using Postgres 8.2.

Thanks for the help.

Christopher J. Bottaro wrote:

new = Blah.new
new.some_field = some_value
new.save! # ERROR: null value in column “modified_at” violates not-
null constraint (ActiveRecord::StatementInvalid)

Is there any way to let ActiveRecord use the default value set at the
database level?

I’m using Postgres 8.2.

I’m not sure if you can use the default from the database. But why are
you using modified_at instead of updated_at which Rails will handle for
you automatically?

Also, if you want to set a default value you could simply do so in a
before_save hook:

def before_save
self.modified_at = Time.now
end


Cheers,

  • Jacob A.

Instead of modified_at you can use updated_at as column name, rails
takes care of it internally.

of as Jacob before_save will work, just check if the value is blank?
first.

Gokhan
www.sylow.net
Jacob A. wrote:

Christopher J. Bottaro wrote:

new = Blah.new
new.some_field = some_value
new.save! # ERROR: null value in column “modified_at” violates not-
null constraint (ActiveRecord::StatementInvalid)

Is there any way to let ActiveRecord use the default value set at the
database level?

I’m using Postgres 8.2.

I’m not sure if you can use the default from the database. But why are
you using modified_at instead of updated_at which Rails will handle for
you automatically?

Also, if you want to set a default value you could simply do so in a
before_save hook:

def before_save
self.modified_at = Time.now
end


Cheers,

  • Jacob A.