我使用i18n API是为了一个目的。我在MySQL数据库中添加了以下内容:
Translation.find_or_create_by(locale: 'en', key:'key1', value: 'value1')但是,种子之后,将数据保存到数据库中,如下所示:
locale: en
key: key1
value: --- value1\n...\n所有列都是varchar(255)和‘utf8 8_unicode_ci’。
在Rails i18n文档中,我找不到对此的解释。
由于这个问题,我不能使用find_or_create_by()方法。它做/不能检查值列并添加重复条目。
有什么解决办法吗?
翻译模式:
Translation = I18n::Backend::ActiveRecord::Translation
if Translation.table_exists?
I18n.backend = I18n::Backend::ActiveRecord.new
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
I18n::Backend::Simple.send(:include, I18n::Backend::Memoize)
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Simple.new, I18n.backend)
end发布于 2018-03-22 13:36:34
您在value列中看到的是值序列化为YAML (由I18n::Backend::ActiveRecord::Translation完成);除其他外,这是多元化所必需的。
当存储在数据库中的值需要序列化时,#find_or_create_by不能很好地工作
做一个简单的种子尝试:
Translation.create_with(value: 'value1').find_or_create_by(locale: 'en', key: 'key1')https://stackoverflow.com/questions/49428225
复制相似问题