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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:25:22+00:00 2026-06-03T12:25:22+00:00

I have a situation in my Rails application where I need to include arbitrary

  • 0

I have a situation in my Rails application where I need to include arbitrary modules depending on the current runtime state. The module provides custom application code that is only needed when certain conditions are true. Basically, I’m pulling the name of a company from the current context and using that as the filename for the module and its definition:

p =  self.user.company.subdomain + ".rb"
if File.exists?(Rails.root + "lib/" + p)
include self.class.const_get(self.user.company.subdomain.capitalize.to_sym)
    self.custom_add_url
end

My test module looks like this:

module Companyx
    def custom_add_url
    puts "Calling custom_add_url"   
end 
end

Now in the console, this actually works fine. I can pull a user and include the module like so:

[1] pry(main)> c = Card.find_by_personal_url("username")
[2] pry(main)> include c.class.const_get(c.user.company.subdomain.capitalize)=> Object
[3] pry(main)> c.custom_add_url

Calling custom_add_url

If I try to run the include line from my model, I get

NoMethodError: undefined method `include' for #<Card:0x007f91f9094fb0>

Can anyone suggest why the include statement would work on the console, but not in my model code?

  • 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-03T12:25:24+00:00Added an answer on June 3, 2026 at 12:25 pm

    Include is a method on a class.

    If you want to call it inside a model, you need to execute the code in the context of its singleton class.

    p =  self.user.company.subdomain + ".rb"
    if File.exists?(Rails.root + "lib/" + p)
    myself = self
    class_eval do
      include self.const_get(myself.user.company.subdomain.capitalize.to_sym)
    end
    self.custom_add_url
    

    EDIT:

    class << self doesn’t accept a block; class_eval does, hence it preserves the state of local variables. I’ve modified my solution to use it.

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

Sidebar

Related Questions

In a toy Rails application, I'm modelling a situation where you have a number
This is not a ruby/rails project deploy. I have the following situation and I
Anybody have any ideas? The situation is like this: I have a primary rails
I have situation where I need to change the order of the columns/adding new
I'm using Ruby on Rails to develop a web application. I have a wiki
This seems like a classic rails situation: I want to have a my index
I am new to rails, and have a situation that I can't quite get
Hi all I have this situation , I need to write unit test cases
Here is my current situation: I have a user class that has an attribute
In my Rails application I have these models Order has_many OrderItem OrderItem belongs_to Order

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.