13

When creating a model in Rails, I forgot to add a column amount that I want. How can I add it to the model later?

2 Answers 2

17

Create a new migration via the console with:

rails g migration add_amount_to_items

This should create a migration something like this:

class AddAmountToItems < ActiveRecord::Migration
  def change
    # add_column table_name, :column_name, :column_type
    add_column :items, :amount, :integer
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

Hello. I did like this "rails g migration add_priority_to_article" - but the migration file that was generated had no instruction... instead I used "priority:integer" (ending the g-command) and it worked
11

The lazy way:

rails g migration add_amount_to_items amount:integer

3 Comments

Hi thank you @WeGoingNowhere - what is the proper, dilligent way that will save grief long term. what if you don't want 'quick and dirty'
And don't forget to run the migration: bin/rake db:migrate
this should be the accepted answer

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.