Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9001113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:23:37+00:00 2026-06-16T00:23:37+00:00

First, let say that my understanding of the Ruby object model is woefully incomplete,

  • 0

First, let say that my understanding of the Ruby object model is woefully incomplete, having come from C, PHP, and Java.

Now, my application contains functionality that is shared by several models, so I’m trying to move that functionality into a module to keep things DRY. Each model has a ‘data’ attribute that needs to be serialized. However, when I move the serialize call into a module, I can’t make it work. Here is the basic model:

class MyModel < ActiveRecord::Base
  include MyApp::Data
end

And here is the the module:

module MyApp
  module Data
    serialize :data, ActiveRecord::Coders::Hstore

    def get_data(key)
      data && data[key]
    end

    def set_data(key, value)
      self.data = (data || {}).merge(key => value)
    end
  end
end

When I try to instantiate a model that includes MyApp:Data, I get the following error:

NoMethodError: undefined method `serialize' for MyApp::Data:Module

How do I make it so that when I instantiate MyModel, the data attribute is serialized by simply including MyApp::Data?

EDIT:

I’ve also tried this:

module MyApp
  module Data
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      serialize :data, ActiveRecord::Coders::Hstore
    end

    def get_data(key)
      data && data[key]
    end

    def set_data(key, value)
      self.data = (data || {}).merge(key => value)
    end
  end
end

Unfortunately, I’m still getting the same error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:23:38+00:00Added an answer on June 16, 2026 at 12:23 am

    As you have written it serialize is called when the module is loaded. The method call to serialize has no receiver so it is made on the Data module. You on the other hand want to call it on the class that is including the module.

    You can do this with the self.included hook – it is called whenever the module is included and the class that is doing the including is passed as an argument, for example

    module Data
      def self.included(base)
        base.serialize ...
      end
    end
    

    With activesupport you can also do

    module Data
      extend ActiveSupport::Concern
    
      included do
        serialize :data, ...
      end
    end
    

    It sets up the self.included for you and evaluates the block such that self will be the including class, not the module (which can be handy if the methods you want to call happen to be protected/private – dsl stuff isn’t as readable when it’s full of base.send :blah)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that there are two PHP functions. function first() { $keyword = $this->input->post('keyword');
Let me first say that I have quite a lot of Java experience, but
First let me say I come from a Microsoft background and Visual Studio is
First let me say that I did see this article: How to remove AspxAutoDetectCookieSupport
I can't figure this one out. At first let me say that my cache
Let’s say that my string is: test! I want to get the first character
First of all, let me say that I am not a professional coder -
First, let me just say that I have searched for information on this topic;
Let's say I have a form that collects a first name and a last
First, we create classes that represent db entities, ok, done. Let's say we use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.