2014-01-26 59 views
1

我在Rails中遇到了一個小問題。我已經安裝了i18n-active_record gem(使用rails 4和ruby 2)。在我的寶石文件i18n-active_record PG ::錯誤:錯誤:關係「翻譯」不存在(導軌4)

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 

現在我可以捆綁運行安裝,和創業板被安裝。但是,如果我嘗試運行耙分貝:遷移我得到我已經先運行遷移,然後加入寶石到Gemfile中並運行包安裝來解決這個事情的錯誤

PG::Error: ERROR: relation "translations" does not exist (and some other stuff) 

在我的本地服務器。但是,gem一定不能在gemfile中,因爲如果是我無法運行rake migrate,因爲gem文件不是最新的。

但現在我想在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 
+0

你創建的模型'Translation'? –

+0

是的。我已經在github上回答了這個問題。我的問題解決了添加我的第一篇文章中編輯後描述的代碼。 Thx無論如何:D –

+0

你自己回答... –

回答

1

我有了這個解決了,我需要添加,如果表中初始化(locale.rb)存在

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 
相關問題