1

I am a newbie in rails; my problem is: I have a table for users, and I need to add more fields...

Where do I put that? I tried to put it in the migration file, but the schema doesn't change when I run rake db:migrate.

This is in my migration file:

def self.up
 create_table :users do |t|
  t.string :username,         :null => false  # if you use another field as a username, for example email, you can safely remove this field.
  t.string :email,            :default => nil # if you use this field as a username, you might want to make it :null => false.
  t.string :crypted_password, :default => nil
  t.string :salt,             :default => nil
  t.string :nombres,          :default => nil
  t.string :apellidos,        :default => nil
  t.string :codigo,           :default => nil
  t.string :fecha,            :default => nil
  t.string :zona,             :default => nil
  t.string :institucion,      :default => nil
  t.string :frecuencia,       :default => nil
  t.string :pregunta,         :default => nil
  t.string :respuesta,        :default => nil



  t.timestamps
  end

And the schema is still without new fields

create_table "users", :force => true do |t|
t.string   "username",                     :null => false
t.string   "email"
t.string   "crypted_password"
t.string   "salt"
t.string   "nombres"
t.string   "apellidos"
t.string   "codigo"
t.string   "fecha"
t.string   "zona"
t.string   "institucion"
t.string   "frecuencia"
t.datetime "created_at",                   :null => false
t.datetime "updated_at",                   :null => false
t.string   "remember_me_token"
t.datetime "remember_me_token_expires_at"
end

What should I do?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
xploshioOn
  • 4,035
  • 2
  • 28
  • 37
  • 2
    Create a *new* migration. Almost any Rails tutorial will discuss this. Or SO post; the first Google hit for me was [this exact question](http://stackoverflow.com/questions/4834809/adding-a-column-to-a-table-in-rails). – Dave Newton Feb 05 '13 at 15:58
  • thanks, that solve my problem.!! but i aave a doubt, if i make a new migration file for solve my problem, later i can delete that file without problems? because the fields are created... in that case, if i want to put my app in a hosting, i just need to have my principal migrate file with all fields that i need? – xploshioOn Feb 05 '13 at 16:35
  • No, do *not* delete your migrations; they are how you roll back. There is no reason to delete them, either. – Dave Newton Feb 05 '13 at 16:36
  • but if i move the app to a hosting, i can make again the migrate file with all the fields and not 2 files, 1 with some fields and other with a correction and more files... thats my situation :) – xploshioOn Feb 05 '13 at 21:48
  • Why bother? And you wouldn't be able to roll back. Please just don't until you are more familiar with the framework. – Dave Newton Feb 05 '13 at 22:00
  • ok, well thanks for the tip.! i wouldn't do it. – xploshioOn Feb 06 '13 at 04:03

2 Answers2

2

Simple migration

You can use a migration generator:
$> rails g migration add_position_to_users position:integer
then run
$> rake db:migrate

More complex migration

or more complex migration which rails also provide:
$> rails g migration add_body_and_pid_to_users body:string:index pid:integer:uniq:index
$> rake db:migrate

More information about migrations can be found at railsguides http://guides.rubyonrails.org/migrations.html

computingfreak
  • 4,939
  • 1
  • 34
  • 51
itsnikolay
  • 17,415
  • 4
  • 65
  • 64
  • can you help me with a new doubt? i need a relationship between tables, how i do that? is something like this http://i45.tinypic.com/vfjtdj.png i have user and loans table created, but how i create the relationship? i hope you can help me – xploshioOn Feb 06 '13 at 19:25
  • for model https://gist.github.com/itsNikolay/4729133 , i thinks you will able to create associations in your conroller =) – itsnikolay Feb 07 '13 at 07:16
1

I think something we're not mentioning here is adding the code into the migration file.

My steps to add a column go something like this:

# rails generate migration AddFieldName blah:string

Inside the migration file generated:

(btw, this typically looks like: db/migrations/20130330115915_add_field_name.rb)

class AddFieldName < ActiveRecord::Migration
    def change
        add_column :table_name, :blah, :string
    end
end

After making this change, I then run db:migrate. Then, the column is added to the database.

Harlin
  • 1,059
  • 14
  • 18