I have a situation in my Rails application where I need to enable arbitrary modules depending on the current state of execution. The module provides custom application code, which is necessary only under certain conditions. Basically, I pull the company name out of the current context and use this as the file name 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 is as follows:
module Companyx def custom_add_url puts "Calling custom_add_url" end end
Now it works fine in the console. I can pull the user out and turn on the module as follows:
[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
Call custom_add_url
If I try to run the include string from my model, I get
NoMethodError: undefined method `include' for #<Card:0x007f91f9094fb0>
Can anyone suggest why the include statement will work on the console, but not in my model code?
source share