Plugin: include vs. base.send :include

Hello all,

I’ve created a simple plugin to extend the functionality of my
controllers. So in my plugins//lib directory I created the
following module:

module MyPlugin
def self.included(base)
include InstanceMethods
end

module InstanceMethods
def some_method

end
end
end

To extend the controllers just put

include MyPlugin

somewhere in ApplicationController and that’s it… I thought!
I found out that ‘include InstanceMethods’ will only work if my
application is running in ‘development’. When running in ‘production’
calling ‘some_method’ will result in a ActionController::UnknownAction
exception.

If I change the ‘include’ line into:
base.send(:include, InstanceMethods)
things will work fine, so after all there’s no problem at all, but I’m
just curious of someone of has the same experience and if there’s a
difference between ‘include’ and ‘base.send :include’?

Thanks in advance.

(I’m using Rails 1.2.6).

On 14 Apr 2008, at 09:02, Patrick B. wrote:

If I change the ‘include’ line into:
base.send(:include, InstanceMethods)
things will work fine, so after all there’s no problem at all, but I’m
just curious of someone of has the same experience and if there’s a
difference between ‘include’ and ‘base.send :include’?

Well it’s all about who’s doing the include, in one case you’re doing
it to MyPlugin (after the module has been included in
ApplicationController), in the other case you’re doing it to
ApplicationController itself (since that’s what Base will be). I
suspect you’re just been lucky: in development mode you keep getting
reloaded, so the first time round the methods aren’t added to
ApplicationController, but they are added to MyPlugin, and the second
and following times since they are in MyPlugin they are also added to
ApplicationController. When class caching is on (ie in production)
things are only loaded once and so the methods aren’t added to the
controllers. (not sure if you’re doing this on purpose, but do you
know that what you’ve got there is equivalent to

module MyPlugin
def some_method
end
end
)

Hi Frederick,

Thanks so much for your response. It made it all so much clearer, and, I
must admit, now reading your explanation, I’ve had to figure it out
myself…

There is no reason for using ‘base.send :include’ instead of just
including the module. To be honest, I changed my code to your
suggestion.

Patrick