How to make Rails load the plugin * after * the application code?

I am trying to write a plugin that defines the MongoMapper model. The problem is that when I start script/console, I get this error:

/home/helder/.rvm/gems/ruby-1.8.7-p249/gems/mongo_mapper-0.8.2/lib/mongo_mapper/connection.rb:29:in `` database ': NameError: uninitialized class variable @@ database_name in MongoMapper :: Connection`

which makes me think that he is trying to load my plugin model before setting up a database connection. How do I download the plugin after the rest of my application code?

+5
source share
1 answer

I will try to solve both the error that I encountered, and the general question, as indicated in the title.

Specific error

, . , , Rails (2.3.8) ActiveRecord , , ( ). , , ( init.rb require d ), .

MongoMapper Rails MongoMapper gems/plugins, (Rails , ). MongoDB , :

MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "#myapp-#{Rails.env}"

if defined?(PhusionPassenger)
   PhusionPassenger.on_event(:starting_worker_process) do |forked|
     MongoMapper.connection.connect_to_master if forked
   end
end

config/initializers, MongoMapper, , (, key), BOOM. , .

: init.rb , , ( lib/ app/models, Rails ). , require d Rails , . db . , , . , .

Rails ?

init.rb , :

config.after_initialize do
  # require my models
  # do this
  # do that
end

config , Rails::Initializer.run config/environment.rb, init.rb Rails - .

, , (Rails::Initializer#after_initialize) , . .

+9

All Articles