ruby include in the plugin 'init' works only in console mode

Hi,   I have a plugin. in which there is a module. In the init of the plugin I have a statement like

#ActiveRecord::Base.send (:include, Module::Plugin) Thing.send (:include, Module::Plugin) #Thing is a model

That however does not work.. and while debugging I find that any call inside the Thing instance methods (rails development mode) returns a method not found..However the same works when I use the script/ console..

Is there some rails quirk here? The include inside the ActiveRecord::Base works but I dont want to include the module all over..

Krishna wrote:

That however does not work.. and while debugging I find that any call inside the Thing instance methods (rails development mode) returns a method not found..However the same works when I use the script/ console..

Is there some rails quirk here? The include inside the ActiveRecord::Base works but I dont want to include the module all over..

Take a look at rails initializers.. your Thing model is not loaded at the time your plugins are loaded..

a good intro is:

http://ryandaigle.com/articles/2007/2/23/what-s-new-in-edge-rails-stop-littering-your-evnrionment-rb-with-custom-initializations

In development, after requests your model gets reloaded, and the reloaded copy never has include called on it. One way of circumventing things is for the model to request all this itself, with the fairly common pattern

class Foo   acts_as_your_plugin end

Fred

Another way is to wrap your init.rb code in a to_prepare block

config.to_prepare do   Thing.send(:include, Module::Plugin) end

This will ensure that this code is run before each request in development mode, and before the first request in production mode.

- James

Thanks..but where do I add the config.to_prepare code? Or should I just wrap the code in the plugin/init.rb within the config.to_prepare? If yes,does it delay this till the Thing module is loaded or does it forcibly load the Thing module?