3

This is my User model.rb

class User < ActiveRecord::Base
    attr_accessor :password
  attr_accessible :email, :name

  validates :name,:presence=>true,:length=>{:maximum=>15}
    validates :email,:presence=>true,:length=>{:maximum=>15}
end

I wanted to add a new column of password. I used the command

rails g migration pass_mig password:string

then

rake db:migrate

but in db schema still

ActiveRecord::Schema.define(:version => 20130627073430) do

      create_table "users", :force => true do |t|
        t.string   "name"
        t.string   "email"
        t.datetime "created_at", :null => false
        t.datetime "updated_at", :null => false
      end
end

Also in Rails console :Password can not be added in a new User object i.e in a new database entry..Please suggest. P.S: I am new to rails so this might be a silly ques. I am using rails version:3.2.13 and ruby version:1.9.3

2 Answers 2

6

Ok, this is one of those "magical" things about rails everyone is always talking about. There is a naming convention for when you do migrations to add columns to tables. Try this:

rails g migration add_password_to_users password:string

There are actually important naming conventions for nearly everything.

Sign up to request clarification or add additional context in comments.

5 Comments

You should also inspect and edit any migration files produced by the generator before executing them.
Hey thanks a lot."Magical" it is. Also can you please tell the best place to learn some other magical conventions from.
user2526795 you'll want to follow the advice @tadman mentions above. And study the code that Bachan Smruty posted as well. There are multiple ways of doing most things in rails.
If you're looking for more advice on how Rails does its thing, there are many books that might provide the sorts of examples you're looking for.
@tadman ok ..thanks.. looks like just the right thing i was looking for
0

You can add the standalone migration as per the below

rails generate migration AddPasswordToUsers password:string

For more standalone migration you can follow the link. http://guides.rubyonrails.org/migrations.html#creating-a-standalone-migration

OR you can do another thing. 1st create a migration

rails g migration AddColumnToUser

This will create a migration file in db/migrate. You need to change that file content.

class AddColumnToUser < ActiveRecord::Migration
  def change
    # all the codes being generated automatically. The following you need to write.
    add_column :users, :password, :string   # this means you are adding password having string type in users table.
  end
end

After done with one of the steps above. just do rake db:migrate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.