Search code examples
ruby-on-railsruby-on-rails-3rails-engines

Rails mountable engine with isolate_namespace but without prefixed namespace on tables


Is there a way to configure the isolate_namespace method to not use prefixed table names?

class Engine < ::Rails::Engine
  isolate_namespace MyEngine
end

Additionally, an isolated engine will set its name according to namespace, so MyEngine::Engine.engine_name will be “my_engine”. It will also set MyEngine.table_name_prefix to “my_engine_”, changing the MyEngine::Article model to use the my_engine_articles table.Isolated Engine Docs

When designing a prototype I ran into an issue where I need the routes to use the isolated namespace pattern, but the database tables do not. This is because the mountable engine I am writing has it's own self contained database.

Don't want to dig much further if it's not possible.


Solution

  • Rails 3 and 4
    Did a little digging into the Rails Engine codebase to find a solution. If you define a method to specify the table name prefix (in /lib/my_engine.rb), it will just use that instead. So set returning nil works fine.

    require "my_engine/engine"
    
    module MyEngine
      # Don't have prefix method return anything.
      # This will keep Rails Engine from generating all table prefixes with the engines name
      def self.table_name_prefix
      end
    end