Forum: Ruby on Rails how to give default value to fields in a model while creating

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
--- Z@m --- (Guest)
on 2009-03-05 15:39
(Received via mailing list)
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


  def self.down
    drop_table :employees

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 i find the default argument is not working

=> #<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
Starr H. (Guest)
on 2009-03-05 15:58
(Received via mailing list)
On Thu, 5 Mar 2009 05:38:24 -0800 (PST)
"--- Z@m ---" <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"

Starr H.
My blog:
Check out my Helpdesk RailsKit:
--- Z@m --- (Guest)
on 2009-03-06 07:18
(Received via mailing list)
yea am using mysql

sad ! !
This topic is locked and can not be replied to.