# How to use â€˜constantizeâ€™ in custom contr oller module

**URL:** <https://www.ruby-forum.com/t/how-to-use-a-constantizea-in-custom-contr-oller-module/173458>\
**Category:** Rails\
**Created:** [August 9, 2009, 6:41pm UTC](https://www.ruby-forum.com/t/how-to-use-a-constantizea-in-custom-contr-oller-module/173458 "2009-08-09T18:41:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![boblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@boblu](https://www.ruby-forum.com/u/boblu)\
**Post date:** [August 9, 2009, 6:41pm UTC](https://www.ruby-forum.com/t/how-to-use-a-constantizea-in-custom-contr-oller-module/173458/1 "2009-08-09T18:41:01Z")

</div>

I have a module like this:

module Controller  
module LocaleModels  
def self.included(base)  
base.send :include, InstanceMethods  
end

```
module InstanceMethods
  def locale_Lexeme; constantize_model('Lexeme') end
  def locale_Synthetic; constantize_model('Synthetic') end
  def locale_Property; constantize_model('Property') end

  private
  def constantize_model(common_part)
    eval(I18n.locale.capitalize + '::' + common_part).constantize
  end
end

```

end  
end

But I kept getting

NoMethodError (undefined method `constantize’ for #Class:0x2483b0c)

I guess I cannot use ‘constantize’ in a custom module.

But can you please offer some workaround?

---

<div class="post-metadata">

**Author:** ![boblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@boblu](https://www.ruby-forum.com/u/boblu)\
**Post date:** [August 9, 2009, 7:56pm UTC](https://www.ruby-forum.com/t/how-to-use-a-constantizea-in-custom-contr-oller-module/173458/2 "2009-08-09T19:56:31Z")

</div>

Hello–  
On Aug 9, 2009, at 9:39 AM, boblu wrote:

> ```
> def locale_Lexeme; constantize_model('Lexeme') end
> 
> ```
> 
> But I kept getting
> 
> NoMethodError (undefined method `constantize’ for #Class:0x2483b0c)
> 
> I guess I cannot use ‘constantize’ in a custom module.
> 
> But can you please offer some workaround?

This may not be the problem, but why are you using eval?  
ActiveSupport::CoreExtensions::String::Inflections defines constantize  
as a method on String. It would seem to me that this would work:

(I18n.locale.capitalize + ‘::’ + common\_part).constantize

However, if you are looking for a class, investigate classify instead.

Maybe there’s something I’m missing here that requires use of eval?  
What happens in script/console if you type the expression in?

---

<div class="post-metadata">

**Author:** ![boblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@boblu](https://www.ruby-forum.com/u/boblu)\
**Post date:** [August 10, 2009, 2:32am UTC](https://www.ruby-forum.com/t/how-to-use-a-constantizea-in-custom-contr-oller-module/173458/3 "2009-08-10T02:32:46Z")

</div>

Thank you for your reply.

Actually, that eval is a typo. But I found by removing that ‘eval’ did  
resolved my problem here.

Thank you very much.
