Skip to content

Instantly share code, notes, and snippets.

@sspross
Created March 28, 2010 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sspross/346740 to your computer and use it in GitHub Desktop.
Save sspross/346740 to your computer and use it in GitHub Desktop.
migration helper to migrate existing data to a target language after adding globalize2 to an existing project
module Globalize2MigrationHelper
def migrate_existing_data_to_language(model, target_language)
say "migrating translated attributes of model '#{model}' to language '#{target_language}'"
# set language to use
I18n.locale = target_language
# iterate over all records
for object in model.all
# iterate over translated attributes
for attribute in model.translated_attribute_names
# fill in original value
# say "setting '#{model}.#{attribute}' to '#{object[attribute]}'"
object.send(:"#{attribute}=", object[attribute])
end
# and save the object without validation
object.save_with_validation(false)
end
end
end
require 'globalize2_migration_helper'
class AddGlobalize2TranslationTablesToModels < ActiveRecord::Migration
extend Globalize2MigrationHelper
def self.up
Book.create_translation_table! :name => :string
migrate_existing_data_to_language(Book, :de)
end
def self.down
Book.drop_translation_table!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment