# Calling class methods from included module

**URL:** <https://www.ruby-forum.com/t/calling-class-methods-from-included-module/156868>\
**Category:** Rails\
**Created:** [January 4, 2009, 5:13pm UTC](https://www.ruby-forum.com/t/calling-class-methods-from-included-module/156868 "2009-01-04T17:13:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [January 4, 2009, 5:13pm UTC](https://www.ruby-forum.com/t/calling-class-methods-from-included-module/156868/1 "2009-01-04T17:13:26Z")

</div>

Hi,

I have these two ActiveRecord models: UserEvent and CoordinateEvent.

The models are extended with the module MapEvent. MapEvent includes some  
class methods and some instance methods.

Now, I want both models to use ActiveRecord’s serialize  
([ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001984)).

My MapEvent looks something like the following. I simply can’t figure  
where to put “serialize :column\_name”:

```auto
module MapEvent
  def self.included(base)
    base.extend(ClassMethods)
  end

  def a_method
  end

  module ClassMethods
    def another_method
    end
  end
end
```

How do I call the serialize method from the above context?

An extra question:  
Also, it suddenly hit me, that instead of a module I could write  
MapEvent as a class that inherits from ActiveRecord instead of a module

- and then let UserEvent and CoordinateEvent inherit from MapEvent?  
Would you recommend this? Generally speaking, when should you use  
mixins/module and when you use inheritance?

Hope my questions are clear. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [January 4, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/calling-class-methods-from-included-module/156868/2 "2009-01-04T17:26:09Z")

</div>

Well - I think I now can answer both of my questions myself. In case  
anyone is curious here is what I’ve learned:

In order to call the serialize method that the two models inherited from  
AR write this:  
module MapEvent  
def self.included(base)  
base.serialize :command  
end  
end

This makes sense as serialize is just an instance method of the models.

And regarding the module/inheritance question I found an answer reading  
these article (including the comments):

> **[ruby, and mix-ins vs. mult inheritance](https://bibwild.wordpress.com/2007/04/10/ruby-and-mix-ins-vs-mult-inheritance/)**
>
> So, I’m afraid I’m joining the ruby cult. Or at least learning ruby. I like a lot of things about it (so nice to work with a really object oriented language, where the community writes …

Modules should generally speaking be used when you just want to add  
methods to your concrete classes. If you want to add new class or  
instance variables use “normal” inheritance.
