Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create a new column in an existing table in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 447
    Comment on it

    This the existing table in migration folder.


    class CreateArticles < ActiveRecord::Migration
      def change
        create_table :articles do |t|
          t.string :title
          t.text :text
          t.timestamps null: false
        end
      end
    end

    Now I have to add column or a new field "Picture" to the table Articles. so, i have to create migration by using this command in the terminal.

    $ rails generate migration AddPictureToArticle picture:binary


    Here, we are generating migration "Add" creates "change" method in Migration, and "ToArticle" indicates the table "articles."


    So, I got a migration file, db/migrate/20160112054236_add_picture_to_article.rb whose contents is:


    class AddPictureToArticle < ActiveRecord::Migration
      def change
        add_column :articles, :picture, :binary
      end
    end

    Now all we have to do is:

    $ rake db:migrate



 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: