# How to include a module in a model

**URL:** <https://www.ruby-forum.com/t/how-to-include-a-module-in-a-model/193743>\
**Category:** Rails\
**Created:** [August 23, 2010, 9:14pm UTC](https://www.ruby-forum.com/t/how-to-include-a-module-in-a-model/193743 "2010-08-23T21:14:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [August 23, 2010, 9:14pm UTC](https://www.ruby-forum.com/t/how-to-include-a-module-in-a-model/193743/1 "2010-08-23T21:14:09Z")

</div>

I created a module MyModule “my\_module.rb” and put in a lib folder in  
my rails app.

In my User.rb model I call include MyModule.

When I try to use a method inside MyModule I get an “undefined method  
error”. How do I include this module in the model?

Thanks

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [August 23, 2010, 9:15pm UTC](https://www.ruby-forum.com/t/how-to-include-a-module-in-a-model/193743/2 "2010-08-23T21:15:46Z")

</div>

badnaam wrote:

> I created a module MyModule “my\_module.rb” and put in a lib folder in  
> my rails app.
> 
> In my User.rb model I call include MyModule.
> 
> When I try to use a method inside MyModule I get an “undefined method  
> error”. How do I include this module in the model?

What you described should work. Let’s see code.

> Thanks

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [August 23, 2010, 9:23pm UTC](https://www.ruby-forum.com/t/how-to-include-a-module-in-a-model/193743/3 "2010-08-23T21:23:31Z")

</div>

[http://pastie.org/1110631](http://pastie.org/1110631)

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [August 23, 2010, 9:45pm UTC](https://www.ruby-forum.com/t/how-to-include-a-module-in-a-model/193743/4 "2010-08-23T21:45:47Z")

</div>

I suspect that after you require validation\_group.rb you want to have  
something like:

ActiveRecord::Base.class\_eval { extend  
ValidationGroup::ActiveRecord::ActsMethod }

You need to get the “validation\_group” method into ActiveRecord::Base  
and therefore User before you call it.

-Rob

On Aug 23, 2010, at 3:22 PM, badnaam wrote:

> > > When I try to use a method inside MyModule I get an "undefined  
> > > –  
> > > Groups “Ruby on Rails: Talk” group.  
> > > To post to this group, send email to rubyonrails-  
> > > removed\_email\_address@domain.invalid.  
> > > To unsubscribe from this group, send email to removed\_email\_address@domain.invalid  
> > > .  
> > > For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en)  
> > > .

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [August 23, 2010, 9:22pm UTC](https://www.ruby-forum.com/t/how-to-include-a-module-in-a-model/193743/5 "2010-08-23T21:22:56Z")

</div>

Here is the code…

[http://pastie.org/1110631](http://pastie.org/1110631)

Thanks

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [August 23, 2010, 9:51pm UTC](https://www.ruby-forum.com/t/how-to-include-a-module-in-a-model/193743/6 "2010-08-23T21:51:38Z")

</div>

Rob,

Are you saying I need to do a

1 - require ‘validation\_group’ in my User.rb

2 - Then add

ActiveRecord::Base.class\_eval { extend  
ValidationGroup::ActiveRecord::ActsMethod }  
to my User.rb?

Thanks  
On Aug 23, 12:44 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [August 23, 2010, 9:59pm UTC](https://www.ruby-forum.com/t/how-to-include-a-module-in-a-model/193743/7 "2010-08-23T21:59:15Z")

</div>

On Aug 23, 2010, at 3:50 PM, badnaam wrote:

> > 
> 
> ActiveRecord::Base.class\_eval { extend  
> ValidationGroup::ActiveRecord::ActsMethod }  
> to my User.rb?
> 
> Thanks

No, I don’t know what you “need” only what I “suspect” your code is  
missing. Probably not in your user.rb, but in an initializer  
somewhere. (config/initializers/validation\_group.rb or in config/  
environment.rb) If you’re modifying the plugin, you really need to  
understand the Ruby hook methods (like extended() and included()) or  
at least copy all the relevant bits when you’re cargo-culting a  
solution.

-Rob

> > > > > I created a module MyModule “my\_module.rb” and put in a lib
> > 
> > > > Posted viahttp://www.ruby-forum.com/.
> 
> .  
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en)  
> .

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [September 2, 2010, 8:42am UTC](https://www.ruby-forum.com/t/how-to-include-a-module-in-a-model/193743/8 "2010-09-02T08:42:28Z")

</div>

ouch. Cargo cult. a bit harsh. At some point we were all learning to  
program. How we treat the newcomer says a lot about the Rails  
community.

badnamm, I think what you’re trying to do might be satisfied by extend  
not include.

see this article for more info.

[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/](http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/)

dave

On Aug 23, 12:58 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
