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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:30:58+00:00 2026-05-26T23:30:58+00:00

I am writing an application where many (but not all) ActiveRecord models have a

  • 0

I am writing an application where many (but not all) ActiveRecord models have a hash column. This is populated on creation with a random MD5 hash, and used for referencing a single object instead of its ID. To achieve this, I have included the following module in the respective models, and use find_by_id_or_hash!() in all controllers instead of find:

module IdOrHashFindable

  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods

    before_create :create_hash             ## <-- THIS FAILS

    # legacy. in use only until find by ID is phased out altogether
    def find_by_id_or_hash!(id_or_hash)
      id_or_hash.to_s.size >= 32 ? find_by_hash!(id_or_hash) : find(id_or_hash)
    end
  end

  def to_param; self.hash end
  def create_hash; self.hash = Support.create_hash  end

end

To keep things DRY, I would like to have the before_create call also inside the module. However, I keep getting either

undefined method `before_create' for IdOrHashFindable:Module

or

undefined method `before_create' for IdOrHashFindable::ClassMethods:Module

depending where I put it. This makes sense (after all, I am calling a function, not defining it), but I’d still like to know how this can be done. (I cannot overwrite before_create since there are other before_create invocations).

Also, for all models which have this module included, quite similar tests apply. How do I consistently test this feature? Do I write a custom describe ... end block and require it into each model_spec.rb where it applies? How do I pass the correct model to use without resorting to global variables?

Any ideas would be appreciated!

  • 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-26T23:30:59+00:00Added an answer on May 26, 2026 at 11:30 pm

    You must place class method invokation in class_eval or just directly invoke it like:

    module IdOrHashFindable
    
      def self.included(base)
        base.extend(ClassMethods)
        base.before_create :create_hash
        # or
        base.class_eval do
          before_create :create_hash
        end
      end
    
    end
    

    because when you place the method in the module, it will directy invoke it as the module’s method

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

Sidebar

Related Questions

I'm writing an application that will have to be able to handle many concurrent
I am writing an application and I will have some dictionary values in many
I'm writing a simple membership application. I have two models, Member and Membership .
I am writing an application that downloads large files in the background. All clients
I am currently writing an financial application, and we have a pretty standard customer
I have a C application with many worker threads. It is essential that these
I'm writing an application for android and have a fairly simple SQLite3 database (see
I am writing a Delphi 2010 application. It has many files, two of which
I am writing a social networking android application. I have create a .Net webservice
When writing application code, it's generally accepted that premature micro -optimization is evil, and

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.