Forum: Ruby on Rails Mixin problems

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2007-07-31 22:40
I'm trying to add a mixin i wrote (ModelExtensions module in
lib/model_extensions.rb) to ActiveRecord::Base so that i can use the
methods in it in all my classes.  (well actually there's only a single
method in there at the moment)

I've added this to environment.rb (right at the  bottom):

require 'model_extensions'
ActiveRecord::Base.send(:extend, ModelExtensions)

And the web server starts ok.  But the mixin just doesn't seem to work -
I get a "missing method" message.

So, i explicitly added the mixin to the required classes, with

require 'model_extensions'

at the top of the model file.  Still no joy.  Modules in lib should work
shouldn't they?  Can anyone see what i'm doing wrong?

thanks in advance
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2007-08-01 05:23
(Received via mailing list)
On 7/31/07, Max Williams <rails-mailing-list@andreas-s.net> wrote:
>
> I'm trying to add a mixin i wrote (ModelExtensions module in
> lib/model_extensions.rb) to ActiveRecord::Base so that i can use the
> methods in it in all my classes.  (well actually there's only a single
> method in there at the moment)
>
> I've added this to environment.rb (right at the  bottom):
>
> require 'model_extensions'
> ActiveRecord::Base.send(:extend, ModelExtensions)

Is the method supposed to be a class method or an instance method? If
it's supposed to be and instance method, try include instead of
extend:

ActiveRecord::Base.send(:include, ModelExtensions)
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2007-08-01 11:07
David Chelimsky wrote:

> Is the method supposed to be a class method or an instance method? If
> it's supposed to be and instance method, try include instead of
> extend:
>
> ActiveRecord::Base.send(:include, ModelExtensions)

That's fixed it!  Thanks very much - i didn't appreciate the subtleties
when i blindly copied someone's example code (a recurring problem for me
with learning rails actually).

cheers!
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2007-08-01 17:59
(Received via mailing list)
On 8/1/07, Max Williams <rails-mailing-list@andreas-s.net> wrote:
> when i blindly copied someone's example code (a recurring problem for me
> with learning rails actually).
>
> cheers!

Cool - glad to help.

Good luck!
David
This topic is locked and can not be replied to.