开发者

Rails Globalize3 gem: How do I add an additional field to the translation table using a migration?

The docs for the Globalize3 gem are clear about how to create a translation table, but I don't see any information about how to add a field to a translation table during a later migration. For example, I initially included Category.create_translation_table! :name => :string when I created my Category model. Now, however, I need to add a translated field to the model.

How do I do that with a Rails migration? I don't see any docs for an alter_translation_table! method or 开发者_Go百科anything similar...


You can do it by hand, something like the following:

class AddNewFieldToYourTable < ActiveRecord::Migration
  def self.up
    change_table(:your_tables) do |t|
      t.string :new_field
    end
    change_table(:your_table_translations) do |t|
      t.string :new_field
    end
  end

  def self.down
    remove_column :your_tables, :new_field
    remove_column :your_table_translations, :new_field
  end
end


With Globalize4, just :

class AddHintToCategory < ActiveRecord::Migration
  def up
    Category.add_translation_fields! hint: :text
  end

  def down
    remove_column :category_translations, :hint
  end
end

Don't forget to add the new field in your model :

translate :name, :hint


https://github.com/globalize/globalize/blob/master/lib/globalize/active_record/migration.rb:34 line (globalize 4)

add_translation_fields!(fields, options)

P.S. Just a typo in a previous comment, 'add_transaction_fields' isn't defined.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜