Railsのgemに少し問題があります。i18n-active_record gemをインストールしました(rails4とruby2を使用)。私のgemファイルで
gem 'i18n-active_record',
:git => 'git://github.com/svenfuchs/i18n-active_record.git',
:require => 'i18n/active_record'
これにはモデルの変換も必要なので、モデルと移行を生成しました
class CreateTranslations < ActiveRecord::Migration
def self.up
create_table :translations do |t|
t.string :locale
t.string :key
t.text :value
t.text :interpolations
t.boolean :is_proc, :default => false
t.timestamps
end
end
def self.down
drop_table :translations
end
end
これでバンドルインストールを実行でき、gemがインストールされます。しかし、rake db:migrateを実行しようとすると、エラーが発生します
PG::Error: ERROR: relation "translations" does not exist (and some other stuff)
私のローカルサーバーでは、最初に移行を実行し、次にgemをgemfileに追加して、バンドルインストールを実行することでこの問題を回避しました。ただし、gemはgemfileに含まれていてはなりません。存在する場合、gemファイルが最新ではないため、rakemigrateを実行できません。
しかし、今はこれをHeroku(または他のサーバー)にプッシュしたいので、毎回これを実行したくありません。このループを回避する方法はありますか?
編集
githubで答えを得ました。私はただする必要がありました:
require 'i18n/backend/active_record'
if ActiveRecord::Base.connection.table_exists? 'translations'
I18n.backend = I18n::Backend::ActiveRecord.new
I18n::Backend::ActiveRecord.send :include, I18n::Backend::Memoize
I18n::Backend::ActiveRecord.send :include, I18n::Backend::Flatten
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
私はこれを解決しました。初期化子(locale.rb)にifテーブルが存在する場合に追加する必要がありました。
require 'i18n/backend/active_record'
if ActiveRecord::Base.connection.table_exists? 'translations'
I18n.backend = I18n::Backend::ActiveRecord.new
I18n::Backend::ActiveRecord.send :include, I18n::Backend::Memoize
I18n::Backend::ActiveRecord.send :include, I18n::Backend::Flatten
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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加