Question

I have a little problem with a gem in Rails. I have installed the i18n-active_record gem (using rails 4 and ruby 2). In my gem file

    gem 'i18n-active_record',
        :git => 'git://github.com/svenfuchs/i18n-active_record.git',
        :require => 'i18n/active_record'

This also requires a model translations so I have a generated a model and a migration

    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

Now I can run bundle install, and the gem gets installed. But if I try to run rake db:migrate I get the error

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

On my local server I have come around this thing by firstly run the migration and then adding the gem into the gemfile and running the bundle install. But the gem must not be in the gemfile, because if it is I cant run the rake migrate, because the gem file is not up to date.

But now I want to push this on Heroku (or any other server) and I really dont want to do this every time. Is there a way for me to get around this loop?

EDIT

I got my answer on github. I just needed to do:

    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
Was it helpful?

Solution

I've got this solved I needed to add the if table exists in the initializer (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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top