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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:23:45+00:00 2026-06-17T09:23:45+00:00

This has been frustrating me for 1/2 a day. I’m trying to extend a

  • 0

This has been frustrating me for 1/2 a day.

I’m trying to extend a Model of mine from a module .rb file located in a non-typical location. In my Model I try extending to a Module based on an Attribute in the Model. The Models are passed to a View, and I want the View to call the same Module method (“content”) in all cases regardless of the Model’s path Attribute.

 Test < ActiveRecord::Base
   ...
   after_initialization do |test|
     if !self.path.nil?
       if File.exists?('app/views/' + self.path + '/_extend.rb')
         extend 'app/views/' + self.path + '/_extend'
       end
     end
   end
   ...
 end

Just trying to dynamically add a class method from a different file. I want to try and keep things organized, which is why I want to stuff all my Module methods into the Model with a giant switch case.

Any suggestions? Thanks.

  • 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-17T09:23:45+00:00Added an answer on June 17, 2026 at 9:23 am

    ruby’s extend method does not work with paths. You need to supply the module you want to extend. So you should not store a path but rather some kind of type that you can later use to get a reference to the module you want to extend. A little example:

    module GuestBehavior
      def has_access?
        false
      end
    end
    
    module AdminBehavior
      def has_access?
        true
      end
    end
    
    class User < ActiveRecord::Base
      after_initialize :extend_behavior
    
      def extend_behavior
        return if kind.blank?
        behavior_module = "#{kind.capitalize}Behavior".constantize
        extend behavior_module
      end
    end
    
    admin = User.new(:kind => 'admin')
    guest = User.new(:kind => 'guest')
    
    admin.has_access? # => true
    guest.has_access? # => false
    

    This is more a though experiment than code I would actually write. It should give you an idea how to achieve your goal.

    EDIT: If you want to put the modules in different places you can easily get it working. Assuming your using rails, there is the autoloader. When you access an undefined constant the autoloader kicks in and tries to load the file, which defines that constat. The above example could look something like:

    app/models/guest_behavior.rb
    app/models/admin_behavior.rb
    

    You don’t need to put any require statements in the code. Rails will automatically load the files when you access GuestBehavior or AdminBehavior. (thats what the constantize call does)

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

Sidebar

Related Questions

This has been frustrating me for days now. I am trying to use Sharepoint
This has been extremely frustrating. I installed a module Finder in Drupal installation. I
This has been frustrating me for a while now. I started developing a site
First of all thanks in advance, this has been very frustrating and I'm hoping
this has been an ongoing problem with me, ive been trying to make a
This has been really frustrating for me so I'll try explain what I know
This has been frustrating me for the better part of an hour. I have
This has been incredibly frustrating, especially given the simplicity of it. Simple javascript: <script
This has been a massive headache. We use Ning as a our platform for
This has been a rather problematic issue on numerous occasions. We have alot of

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.