# Default column values and creating new rows with ActiveRecor

**URL:** <https://www.ruby-forum.com/t/default-column-values-and-creating-new-rows-with-activerecor/105042>\
**Category:** Rails\
**Created:** [May 25, 2007, 7:46am UTC](https://www.ruby-forum.com/t/default-column-values-and-creating-new-rows-with-activerecor/105042 "2007-05-25T07:46:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [May 25, 2007, 7:46am UTC](https://www.ruby-forum.com/t/default-column-values-and-creating-new-rows-with-activerecor/105042/1 "2007-05-25T07:46:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [May 25, 2007, 10:09am UTC](https://www.ruby-forum.com/t/default-column-values-and-creating-new-rows-with-activerecor/105042/2 "2007-05-25T10:09:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [May 25, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/default-column-values-and-creating-new-rows-with-activerecor/105042/3 "2007-05-25T15:56:25Z")

</div>

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](http://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.
