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 7068019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:16:22+00:00 2026-05-28T05:16:22+00:00

I have defined a module to extend ActiveRecord. In my case I have to

  • 0

I have defined a module to extend ActiveRecord.

In my case I have to generate instance methods with the symbols given as arguments to the compound_datetime class method. It works when class_eval is called outside the each block but not inside it; in the latter case I get an undefined method error.

Does anyone know what I am doing wrong?

module DateTimeComposer
  mattr_accessor :attrs
  @@attrs = []

  module ActiveRecordExtensions
    module ClassMethods
      def compound_datetime(*attrs)
        DateTimeComposer::attrs = attrs
        include ActiveRecordExtensions::InstanceMethods
      end
    end

    module InstanceMethods
      def datetime_compounds
        DateTimeComposer::attrs
      end

      def self.define_compounds(attrs)
        attrs.each do |attr|
          class_eval <<-METHODS
            def #{attr.to_s}_to()
              puts 'tes'
            end
          METHODS
        end
      end

      define_compounds(DateTimeComposer::attrs)
    end
  end
end


class Account < ActiveRecord::Base
  compound_datetime :sales_at, :published_at
end

When I try to access the method:

Account.new.sales_at_to

I get a MethodError: undefined method sales_at_to for #<Account:0x007fd7910235a8>.

  • 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-05-28T05:16:23+00:00Added an answer on May 28, 2026 at 5:16 am

    You are calling define_compounds(DateTimeComposer::attrs) at the end of the InstanceMethods module definition. At that point in the code, attrs is still an empty array, and self is the InstanceMethods module.

    This means no methods will be defined, and even if they were, they would be bound to InstanceMethods‘s metaclass, making them class methods of that module, not instance methods of your Account class.

    This happens because method calls inside the InstanceMethods module definition are evaluated as they are seen by the ruby interpreter, not when you call include ActiveRecordExtensions::InstanceMethods. An implication of this is that it is possible to run arbitrary code in the most unusual of places, such as within a class definition.

    To solve the problem, you could use the included callback provided by ruby, which is called whenever a module is included in another:

    module InstanceMethods
      # mod is the Class or Module that included this module.
      def included(mod)
        DateTimeComposer::attrs.each do |attr|
          mod.instance_eval <<-METHODS
            def #{attr.to_s}_to
              puts 'tes'
            end
          METHODS
        end
      end
    end
    

    As an additional suggestion, you should be able to achieve the same result by simply defining the methods when compound_datetime is called, thus eliminating the dependence on the attrs global class variable.

    However, if you must have access to the fields which were declared as compound datetime, you should use class instance variables, which are unique to each class and not shared on the hierarchy:

    module ClassMethods
      def compound_datetime(*attrs)
        @datetime_compounds = attrs
        attrs.each do |attr|
          instance_eval <<-METHODS
            def #{attr.to_s}_to
              puts 'tes'
            end
          METHODS
        end
      end
    
      def datetime_compounds; @datetime_compounds; end;
    end
    
    class Account < ActiveRecord::Base
      compound_datetime :sales_at, :published_at
    end
    
    class AnotherModel < ActiveRecord::Base
      compound_datetime :attr1, :attr2
    end
    
    Account.datetime_compounds
     => [ :sales_at, :published_at ]
    
    AnotherModel.datetime_compounds
     => [ :attr1, :attr2 ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python module that defines a number of classes: class A(object): def
I have defined an interface in C++, i.e. a class containing only pure virtual
I'm really struggling with something here. I have a class module, let's call it
I have defined my helper function in Helper: module CarsHelper def my_helper ... end
I have a defined idl file, which looks like this: module Banking { typedef
I have a file context.ml where a map is defined module CtxMap = Map.make(struct
I have a model object which subclasses ActiveRecord. Additionally, using STI, I have defined
I have a model as follows: class Property < ActiveRecord::Base has_and_belongs_to_many :property_values end What
I am writing a module to extend the Core/Catalog/Product/View/Media.php class so I can expose
I have the following: module Thing def self.included(base) base.send :extend, ClassMethods end module ClassMethods

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.