# Soft-coding model names?

**URL:** <https://www.ruby-forum.com/t/soft-coding-model-names/144419>\
**Category:** Rails\
**Created:** [July 29, 2008, 2:50pm UTC](https://www.ruby-forum.com/t/soft-coding-model-names/144419 "2008-07-29T14:50:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 29, 2008, 2:50pm UTC](https://www.ruby-forum.com/t/soft-coding-model-names/144419/1 "2008-07-29T14:50:42Z")

</div>

Hello.

I am trying to “soft-code” the model names in my app so I can use a  
generic method call to achieve some results. Many of my modules have  
the the following method:

def ModelName.get\_array

# same code goes here for every module and it returns an array.

end

Is there a way to replace the model name with a variable or something  
else in such a way that the method can be called in a generic way?  
Something like:

my\_array = @model\_name.get\_array

Thanks.

Pepe

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 29, 2008, 3:27pm UTC](https://www.ruby-forum.com/t/soft-coding-model-names/144419/2 "2008-07-29T15:27:36Z")

</div>

On Tue, Jul 29, 2008 at 7:49 AM, pepe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to replace the model name with a variable or something  
> else in such a way that the method can be called in a generic way?  
> Something like:
> 
> my\_array = @model\_name.get\_array

Take a look at constantize:  
[http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M000488](http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M000488)

You should be able to do something like this  
@model\_name.constantize.get\_array

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 29, 2008, 5:29pm UTC](https://www.ruby-forum.com/t/soft-coding-model-names/144419/3 "2008-07-29T17:29:36Z")

</div>

This might be better written as class level methods then and mixed at  
the class level. Here’s an example of how that would work:

module Foo  
def self.included(base)  
base.extend ClassLevelMethods  
end

# You can still just define instance level methods as normal

def bar  
# …  
end

# But now these methods will be mixed in at the class level

module ClassLevelMethods  
def baz  
# …  
end  
end  
end

and just include Foo as normal. HTH

RSL

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 30, 2008, 5:30am UTC](https://www.ruby-forum.com/t/soft-coding-model-names/144419/4 "2008-07-30T05:30:47Z")

</div>

Thanks Russell. I understand (or almost) what you have in mind. It  
would probably be the the ‘right’ way to do it. I’ll play with it and  
see what I can come up with.

Thanks a lot.

Pepe

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 30, 2008, 5:29am UTC](https://www.ruby-forum.com/t/soft-coding-model-names/144419/5 "2008-07-30T05:29:02Z")

</div>

Thanks for the tip. I have tried ‘constantize’ with variables and it  
worked like a charm. I’ll try it with my models and see how it works.

Do you know how to do something similar with variables? I have a page  
with a table of 31 rows and all the field names follow the format  
‘field\_namexx’ where ‘xx’ is a sequence number. I have tried  
constantize with regular variables but it only works with…  
constants! Go figure! 🙂

Thanks a lot.

Pepe
