# Ruby include in the plugin 'init' works only in console mode

**URL:** <https://www.ruby-forum.com/t/ruby-include-in-the-plugin-init-works-only-in-console-mode/173495>\
**Category:** Rails\
**Created:** [August 10, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/ruby-include-in-the-plugin-init-works-only-in-console-mode/173495 "2009-08-10T15:09:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krishna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/71c47a/32.png) [@krishna](https://www.ruby-forum.com/u/krishna)\
**Post date:** [August 10, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/ruby-include-in-the-plugin-init-works-only-in-console-mode/173495/1 "2009-08-10T15:09:02Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![krishna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/71c47a/32.png) [@krishna](https://www.ruby-forum.com/u/krishna)\
**Post date:** [August 10, 2009, 3:17pm UTC](https://www.ruby-forum.com/t/ruby-include-in-the-plugin-init-works-only-in-console-mode/173495/2 "2009-08-10T15:17:33Z")

</div>

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](http://ryandaigle.com/articles/2007/2/23/what-s-new-in-edge-rails-stop-littering-your-evnrionment-rb-with-custom-initializations)

---

<div class="post-metadata">

**Author:** ![krishna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/71c47a/32.png) [@krishna](https://www.ruby-forum.com/u/krishna)\
**Post date:** [August 10, 2009, 3:39pm UTC](https://www.ruby-forum.com/t/ruby-include-in-the-plugin-init-works-only-in-console-mode/173495/3 "2009-08-10T15:39:28Z")

</div>

On Aug 10, 2:08 pm, Krishna [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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…

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

---

<div class="post-metadata">

**Author:** ![krishna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/71c47a/32.png) [@krishna](https://www.ruby-forum.com/u/krishna)\
**Post date:** [August 10, 2009, 4:30pm UTC](https://www.ruby-forum.com/t/ruby-include-in-the-plugin-init-works-only-in-console-mode/173495/4 "2009-08-10T16:30:59Z")

</div>

On Aug 10, 2:38 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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

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

---

<div class="post-metadata">

**Author:** ![krishna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/71c47a/32.png) [@krishna](https://www.ruby-forum.com/u/krishna)\
**Post date:** [August 11, 2009, 12:55pm UTC](https://www.ruby-forum.com/t/ruby-include-in-the-plugin-init-works-only-in-console-mode/173495/5 "2009-08-11T12:55:00Z")

</div>

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?
