# how to give default value to fields in a model while creating 	model\_name.new()

**URL:** <https://discuss.rubyonrails.org/t/how-to-give-default-value-to-fields-in-a-model-while-creating-model-name-new/40083>\
**Category:** rubyonrails-talk\
**Created:** [March 5, 2009, 1:38pm UTC](https://discuss.rubyonrails.org/t/how-to-give-default-value-to-fields-in-a-model-while-creating-model-name-new/40083 "2009-03-05T13:38:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Z1](https://avatars.discourse-cdn.com/v4/letter/_/f475e1/32.png) [@\_Z1](https://discuss.rubyonrails.org/u/_Z1)\
**Post date:** [March 5, 2009, 1:38pm UTC](https://discuss.rubyonrails.org/t/how-to-give-default-value-to-fields-in-a-model-while-creating-model-name-new/40083/1 "2009-03-05T13:38:24Z")

</div>

am a newbie to rails , I created a model named Employee

and in migration i added the following

class CreateEmployee \< ActiveRecord::Migration &nbsp;&nbsp;def self.up &nbsp;&nbsp;&nbsp;&nbsp;create\_table :employees do |t|

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.string :name, :limit =\> 40, :null =\> false,:default =\> "" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.text :address, :limit =\> 200,:default =\> "" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.text :location\_name, :default =\> "",:limit =\> 200 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.text :city, :default =\> "",:limit =\> 200 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.text :state, :default =\> "",:limit =\> 200 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.integer :zipcode, :limit =\> 6

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.timestamps &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;def self.down &nbsp;&nbsp;&nbsp;&nbsp;drop\_table :employees &nbsp;&nbsp;end end

i gave default to almost all the fields esp which are string or text

but when i took the console and created a new instance of the class say Employee.new i find the default argument is not working

> > Employee.new

=\> #\<Employee id: nil, name: "", address: nil, location\_name: nil, city: nil, state: nil, zipcode: nil, created\_at: nil, updated\_at: nil\>

> >

only the name is given a default value of "" rest is still nill

---

<div class="post-metadata">

**Author:** ![Starr\_Horne](https://avatars.discourse-cdn.com/v4/letter/s/b19c9b/32.png) [@Starr\_Horne](https://discuss.rubyonrails.org/u/Starr_Horne)\
**Post date:** [March 5, 2009, 1:57pm UTC](https://discuss.rubyonrails.org/t/how-to-give-default-value-to-fields-in-a-model-while-creating-model-name-new/40083/2 "2009-03-05T13:57:40Z")

</div>

Are you using mysql? If so, it doesn't support default values for "text" columns.

SH

---

<div class="post-metadata">

**Author:** ![\_Z1](https://avatars.discourse-cdn.com/v4/letter/_/f475e1/32.png) [@\_Z1](https://discuss.rubyonrails.org/u/_Z1)\
**Post date:** [March 6, 2009, 5:18am UTC](https://discuss.rubyonrails.org/t/how-to-give-default-value-to-fields-in-a-model-while-creating-model-name-new/40083/3 "2009-03-06T05:18:12Z")

</div>

yea am using mysql

sad ! !
