2

The rails globalize gem docs are great, but I can't find a solution, what I have to do, when I want to rename a column.

Last Year I did that, to add the translation fields.

  def up
    remove_column :news, :name
    News.add_translation_fields! name: :string
  end

  def down
    add_column :news, :name, :string, default: nil
    remove_column :news_translations, :name
  end

Now I want to rename the column "name" to "title", without loosing my data and translations. How do I have to write the migration file?

Archer
  • 1,062
  • 1
  • 13
  • 32
  • 1
    Am I missing something here?... Can you not just do `def change; rename_column :news, :name, :title; end`? – Tom Lord Nov 08 '16 at 22:22
  • That. If it's actually more complex then there are a number of solutions that boil down to http://stackoverflow.com/q/6135600/438992 somewhere under the covers. – Dave Newton Nov 08 '16 at 22:23
  • mhh, But I don't want to rename the News model, just the column. – Archer Nov 08 '16 at 22:25
  • `rename_column :news, :name` means "in the news table, rename the name column" - it won't rename the table – Taryn East Nov 08 '16 at 22:31

1 Answers1

2

Alter the column on the news translations table directly:

def change
  rename_column :news_translations, :name, :title
end
rebagliatte
  • 2,110
  • 1
  • 20
  • 25