How to change a column in my database with Ruby on Rails?


Accidentally working with rails wrong name a column of my payments table since I wanted to call it Incoming and I finished it calling ntrantes.

My question is: How can I change the name of the column with the migrations? Or does it have to be manual from the database?

asked by Cesar A. 25.11.2016 в 07:12

1 answer


You can do it in 2 ways, if you do it from the database you will have to change it manually and then update it with rails db:schema:dump .

To update your schema , however, you will have to modify the models and controllers. If on the other hand you like to do it with a migration, you will first need to generate it with:

rails generate migration cambiando_columna

... and in the migration that I generate you put the following (it is wrong in rename_colum , it is rename_column ):

class CambiandoColumna< ActiveRecord::Migration[5.0]
 def change
    rename_column :pagos, :ntrantes, :Entrantes

... and when you finish modifying this migration simply put the command rails db:migrate for this change to take effect.

answered by 25.11.2016 / 07:25