【问题标题】:hstore_translate existing data migrationhstore_translate 现有数据迁移
【发布时间】:2015-03-23 11:32:01
【问题描述】:

我已将 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 总是为零。回退不起作用还是我需要先迁移数据?

更新

此迁移解决了问题。

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

【问题讨论】:

    标签: ruby-on-rails rails-i18n hstore globalize3


    【解决方案1】:

    hstore_translate 定义了读取各种翻译的方法。

    在定义translates :subtitle, :description 时,会创建subtitle & description 方法,从而获得合适的翻译。因此,当调用Product.subtitle 时,它不是获取列及其数据,而是调用方法。

    要临时访问数据,您应该注释掉 hstore_translate 设置

    class Product < ActiveRecord::Base
      # translates :subtitle, :description
    end
    

    然后将您的数据合并到新列,然后再次取消注释。

    【讨论】:

      猜你喜欢
      • 2017-11-22
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多