I've added hstore_translate to a Rails4 project with existing data.
class Product < ActiveRecord::Base
translates :subtitle, :description
end
config.i18n.fallbacks = true
class AddTranslationColumnsToProducts < ActiveRecord::Migration
def change
add_column :products, :subtitle_translations, :hstore
add_column :products, :description_translations, :hstore
end
end
How can I access my old subtitle and description fields? Because now Post.subtitle and Post.description always nil. Fallback doesn't work or I need migrate data first?
upd:
This migration solves problem.
class MigrateExistingDataToTranslations < ActiveRecord::Migration
def change
execute "UPDATE products p SET subtitle_translations=hstore('en',(select subtitle from products where id = p.id));"
execute "UPDATE products p SET description_translations=hstore('en', (select description from products where id = p.id));"
end
end
hstore_translate defines methods to read the various translations.
When defining translates :subtitle, :description
, the methods subtitle
& description
are created, which gets the fitting translation. So when calling Product.subtitle
, instead of getting the column and its data it is calling the method.
To access the data temporarily you should comment out the hstore_translate setup
class Product < ActiveRecord::Base
# translates :subtitle, :description
end
Then merge your data to the new columns before uncommenting again.