我已将hstore_translate添加到包含现有数据的Rails4项目中。
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
如何访问旧的字幕和说明字段?因为现在Post.subtitle和Post.description总是为零。后备不起作用或我需要先迁移数据?
UPD :
此迁移解决了问题。
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
答案 0 :(得分:1)
hstore_translate定义了阅读各种翻译的方法。
定义translates :subtitle, :description
时,方法subtitle
&amp;创建description
,获取拟合翻译。因此,在调用Product.subtitle
时,不是获取列及其数据,而是调用方法。
要暂时访问数据,您应该注释掉hstore_translate设置
class Product < ActiveRecord::Base
# translates :subtitle, :description
end
然后在再次取消注释之前将数据合并到新列。