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

**URL:** <https://www.ruby-forum.com/t/how-to-give-default-value-to-fields-in-a-model-while-creating-model-name-new/162072>\
**Category:** Rails\
**Created:** [March 5, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/how-to-give-default-value-to-fields-in-a-model-while-creating-model-name-new/162072 "2009-03-05T14:39:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [March 5, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/how-to-give-default-value-to-fields-in-a-model-while-creating-model-name-new/162072/1 "2009-03-05T14:39:22Z")

</div>

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

and in migration i added the following

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

```
  t.string :name, :limit => 40, :null => false,:default => ""
  t.text :address, :limit => 200,:default => ""
  t.text :location_name, :default => "",:limit => 200
  t.text :city, :default => "",:limit => 200
  t.text :state, :default => "",:limit => 200
  t.integer :zipcode, :limit => 6

  t.timestamps
end

```

end

def self.down  
drop\_table :employees  
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:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [March 5, 2009, 2:58pm UTC](https://www.ruby-forum.com/t/how-to-give-default-value-to-fields-in-a-model-while-creating-model-name-new/162072/2 "2009-03-05T14:58:14Z")

</div>

On Thu, 5 Mar 2009 05:38:24 -0800 (PST)  
“— Z@m —” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> t.text :address, :limit => 200,:default => ""
> t.text :location_name, :default => "",:limit => 200
> t.text :city, :default => "",:limit => 200
> t.text :state, :default => "",:limit => 200
> t.integer :zipcode, :limit => 6
> 
> ```

Are you using mysql? If so, it doesn’t support default values for “text”  
columns.

## SH

Starr H.  
My blog: [http://starrhorne.com](http://starrhorne.com)  
Check out my Helpdesk RailsKit: [http://railskits.com/helpdesk/](http://railskits.com/helpdesk/)

---

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [March 6, 2009, 6:18am UTC](https://www.ruby-forum.com/t/how-to-give-default-value-to-fields-in-a-model-while-creating-model-name-new/162072/3 "2009-03-06T06:18:58Z")

</div>

yea am using mysql

sad ! !
