Rails 3 Plugin - Active Record table_name_prefix

Im having problem to use table_name_prefix on my projects. I have a main apllication in my project that have others applications as plugins, these plugins works like a subsystem from the main application.

To organize the tables on database of the subsystems I would like to use the table_name_prefix of the ActiveRecord Plugin.

If I put on init.rb of plugin the command config.active_record.table_name_prefix = "per_" the main application will not work because the ActiveRecord will try to find for "per_users" but the only thing I want is that only the Plugin on my main application use the prefix "per_".

I tried to create a rails folder at my plugin with the command above but the same problem occurs, all the application try to find for prefixed table name.

An alternative is use the set_table_name in the model of plugin, but its not good for me because Im developing subsystems as rails plugin and I dont want to change the models when put the subsystem at the main application.

Someone can help me?

Answers


To have each plugin with own prefix, for Rails 3, try organize Your models inside plugin in namespace:

class Foo::Bar < ActiveRecord::Base
 ...
end

module Foo
  def self.table_name_prefix
    'foo_'
  end
end

This will works just inside the plugin without changing anything inside the main application. Other approach is to use some main model and inherit it from others like that:

class Foo < ActiveRecord::Base
  def self.table_name_prefix
    'foo_'
  end
end

class Bar < Foo
 ...
end

sometimes this approach is used to extend all models with extra features.

More information in Rails documentation here


Take a look at this question. I ran into the same problem, forgot my application's name (main module) had the same name as a namespace for my models.


Need Your Help

swift code crash ,log : dyld: Library not loaded: @rpath/libswiftCore.dylib

ios swift crash ios8 xcode6

I debug a simple swift app , it can normally running in the simulator, but on a real machine running on the crash immediately .