Skip to content

Instantly share code, notes, and snippets.

@iboard
Last active January 20, 2019 08:04
Show Gist options
  • Save iboard/3297902 to your computer and use it in GitHub Desktop.
Save iboard/3297902 to your computer and use it in GitHub Desktop.
obsolete: Rake-task to initialize localized fields with MongoId
#
# I had a model where the fields banner_title and banner_text was simple text-fields
# Then I added :localized => true to this fields and all specs pass because specs use new data.
# On my production-server the app crashed because of "nil-fields".
#
# I figured out how to solve this by initializing the _translations for existing records.
# Feel free to use my example in your production-environment - but, sure, without any warranty.
#
# [email protected], August 8, 2012
#
namespace :migrations
desc "Modify data between versions > August 8, 2012 "
task :localize_banner_fields => :environment do
Page.all.each do |page|
page.versionless do |_page|
_old_title = _page.banner_title_translations
_old_text = _page.banner_text_translations
_new_title = _old_title
_new_text = _new_title
unless _old_title.class == BSON::OrderedHash
_new_title = { "en" => _old_title }
puts "CONVERTING #{_old_title.inspect} TO #{_new_title.inspect}"
end
unless _old_text.class == BSON::OrderedHash
_new_text = { "en" => _old_text }
puts "CONVERTING #{_old_text.inspect} TO #{_new_text.inspect}"
end
_page.banner_title_translations = _new_title
_page.banner_text_translations = _new_text
_page.save
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment