# How do set default value for ActiveRecord fields?

**URL:** <https://www.ruby-forum.com/t/how-do-set-default-value-for-activerecord-fields/100834>\
**Category:** Rails\
**Created:** [August 1, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/how-do-set-default-value-for-activerecord-fields/100834 "2006-08-01T13:26:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [August 1, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/how-do-set-default-value-for-activerecord-fields/100834/1 "2006-08-01T13:26:31Z")

</div>

Everytime a new record object is created i.e. Record.new, i want certain  
fields to be the same.

Any idea how to do this?  
Thanks  
Chris

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [August 1, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/how-do-set-default-value-for-activerecord-fields/100834/2 "2006-08-01T13:49:06Z")

</div>

On Aug 01, 2006, at 12:26 pm, Chris wrote:

> Everytime a new record object is created i.e. Record.new, i want  
> certain  
> fields to be the same.
> 
> Any idea how to do this?  
> Thanks  
> Chris

Chris,

You want to set defaults in the database columns. The easiest way to  
do this is to build your database with Rails migrations: http://  
[wiki.rubyonrails.org/rails/pages/UnderstandingMigrations](http://wiki.rubyonrails.org/rails/pages/UnderstandingMigrations) and  
use :default =\> XXX in your column definitions.

Ashley

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [August 1, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/how-do-set-default-value-for-activerecord-fields/100834/3 "2006-08-01T13:49:31Z")

</div>

On Tuesday, August 01, 2006, at 1:26 PM, Chris wrote:

> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Easiest way would be to define :default=\>some\_value in the migration for  
each column

t.column “title”, :string, :default=\>‘test’

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [August 1, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/how-do-set-default-value-for-activerecord-fields/100834/4 "2006-08-01T15:02:07Z")

</div>

Chris wrote:

> So when you create a new activerecord instance (before you save it) does  
> it look to see what the default value is from the schema?
> 
> I want the default value to be assigned upon creation of the object,  
> i.e. Record.new, and not when it gets saved.

Set the default value in the controller.

eg.  
def firstpage  
Tablename.column = default\_value  
end

I havn’t looked at ruby for a while but its something like that.  
Does this help?

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [August 1, 2006, 2:07pm UTC](https://www.ruby-forum.com/t/how-do-set-default-value-for-activerecord-fields/100834/5 "2006-08-01T14:07:35Z")

</div>

So when you create a new activerecord instance (before you save it) does  
it look to see what the default value is from the schema?

I want the default value to be assigned upon creation of the object,  
i.e. Record.new, and not when it gets saved.

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [August 1, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/how-do-set-default-value-for-activerecord-fields/100834/6 "2006-08-01T15:59:46Z")

</div>

On 2006-08-01, at 10:02 , Simon wrote:

> Chris wrote:
> 
> > So when you create a new activerecord instance (before you save  
> > it) does  
> > it look to see what the default value is from the schema?

It does. That’s also why you can’t have expressions as defaults (eg.  
NOW()), just literals.

> > I want the default value to be assigned upon creation of the object,  
> > i.e. Record.new, and not when it gets saved.

Yep. Rails will look it up, it’ll be set at instantiation.

> Set the default value in the controller.

Bad idea. Let the model handle it.

I’m thinking about writing a plugin that initializes default values.  
It’s useful when you have more complex defaults, e.g. with  
inheritance, that each descendant model may have a different default  
for the same field. Sintax would be something like

class Post \< AR…  
defaults :title =\> ‘(untitled)’, :status =\> ‘unpublished’,  
:whatever =\> lambda { |record| … some dynamic magic … }  
end

You can also do similar just by overriding the accessors.
