Add column to a Ruby scaffold

0

I am a beginner in Ruby and I have the following question: after having generated a scaffold called negotiations with the fields name: string last names: string I have noticed that I am missing one more, age: integer. I tried to generate it manually and then I ran rake db: migrate. However, do not fuck me. How can I solve it?

    
asked by user87071 15.05.2018 в 22:17
source

1 answer

0

If you have already executed the migrations and you want to add the column, you can do it through another migration; for example:

rails generate migration AddEdadToNegociaciones edad:integer

What will generate a new migration with the following content:

class AddEdadToNegocaciones < ActiveRecord::Migration[5.1]
  def change
    add_column :negocaciones, :edad, :integer
  end
end

Now you should only execute your migrations again:

$ rails db:migrate

If you do not want to generate a new migration and you prefer to update the one generated by the scaffold , you can do it but you will first have to do a rollback so you can detect the change:

$ rails db:rollback

After the rollback update the migration:

class CreateNegociaciones < ActiveRecord::Migration[5.1]
  def change
    create_table :negociaciones do |t|
      t.string :nombre
      t.string :apellidos
      t.integer :edad

      t.timestamps
    end
  end
end

And, finally, execute the migrations again:

$ rails db:migrate
    
answered by 15.05.2018 / 22:44
source