# Define a string as a Model object

**URL:** <https://www.ruby-forum.com/t/define-a-string-as-a-model-object/182984>\
**Category:** Rails\
**Created:** [February 5, 2010, 5:11pm UTC](https://www.ruby-forum.com/t/define-a-string-as-a-model-object/182984 "2010-02-05T17:11:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_in\_Tennessee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dec6dc/32.png) [@Frank\_in\_Tennessee](https://www.ruby-forum.com/u/Frank_in_Tennessee)\
**Post date:** [February 5, 2010, 5:11pm UTC](https://www.ruby-forum.com/t/define-a-string-as-a-model-object/182984/1 "2010-02-05T17:11:44Z")

</div>

Good morning,

I’m trying to dynamically define models.

This method #1 works:  
model\_name = User  
@Model = model\_name  
@Model.find(params[:id]).name

This method #2 doesn’t:  
model\_name = “User”  
@Model = model\_name  
@Model.find(params[:id]).name

Anyone know how I can get method #2 to work? Is there something I can  
tack onto the end of ‘model\_name’ to get it to know that it’s a model?

Thanks,

Frank

---

<div class="post-metadata">

**Author:** ![Frank\_in\_Tennessee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dec6dc/32.png) [@Frank\_in\_Tennessee](https://www.ruby-forum.com/u/Frank_in_Tennessee)\
**Post date:** [February 5, 2010, 5:16pm UTC](https://www.ruby-forum.com/t/define-a-string-as-a-model-object/182984/2 "2010-02-05T17:16:50Z")

</div>

Try the eval method.

For instance:

classname = “Date”  
=\> “Date”  
eval(classname).today  
=\> Fri, 05 Feb 2010

---

<div class="post-metadata">

**Author:** ![Frank\_in\_Tennessee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dec6dc/32.png) [@Frank\_in\_Tennessee](https://www.ruby-forum.com/u/Frank_in_Tennessee)\
**Post date:** [February 5, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/define-a-string-as-a-model-object/182984/3 "2010-02-05T17:32:31Z")

</div>

Frank\_in\_Tennessee wrote:

> Good morning,
> 
> I’m trying to dynamically define models.
> 
> This method #1 works:  
> model\_name = User  
> @Model = model\_name  
> @Model.find(params[:id]).name
> 
> This method #2 doesn’t:  
> model\_name = “User”  
> @Model = model\_name  
> @Model.find(params[:id]).name

Of course not. In method 2, @Model is a String, not a Class.

> Anyone know how I can get method #2 to work? Is there something I can  
> tack onto the end of ‘model\_name’ to get it to know that it’s a model?

Look up the constantize method.

> Thanks,
> 
> Frank

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Frank\_in\_Tennessee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dec6dc/32.png) [@Frank\_in\_Tennessee](https://www.ruby-forum.com/u/Frank_in_Tennessee)\
**Post date:** [February 5, 2010, 5:43pm UTC](https://www.ruby-forum.com/t/define-a-string-as-a-model-object/182984/4 "2010-02-05T17:43:21Z")

</div>

Works great now. Thank you.

Modified Code:

model\_name = “User”  
@Model = model\_name.constantize  
@Model.find(params[:id]).name

---

<div class="post-metadata">

**Author:** ![Frank\_in\_Tennessee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dec6dc/32.png) [@Frank\_in\_Tennessee](https://www.ruby-forum.com/u/Frank_in_Tennessee)\
**Post date:** [February 5, 2010, 5:41pm UTC](https://www.ruby-forum.com/t/define-a-string-as-a-model-object/182984/5 "2010-02-05T17:41:35Z")

</div>

On Feb 5, 2010, at 11:16 AM, Sharagoz – wrote:

> Try the eval method.
> 
> For instance:
> 
> classname = “Date”  
> =\> “Date”  
> eval(classname).today  
> =\> Fri, 05 Feb 2010

With ActiveSupport you get constantize.

irb\> constantize(“Date”)  
=\> Date

You can either look at the ActiveSupport implementation or this one:

# from Jim W. (based on email correspondence), improved by

Rick Denatale (in ruby-talk:332670)  
def constantize(camel\_cased\_word)  
camel\_cased\_word.  
sub(/^::/,‘’).  
split(“::”).  
inject(Object) { |scope, name| scope.const\_defined?(name) ?  
scope.const\_get(name) : scope.const\_missing(name) }  
end

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Frank\_in\_Tennessee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dec6dc/32.png) [@Frank\_in\_Tennessee](https://www.ruby-forum.com/u/Frank_in_Tennessee)\
**Post date:** [February 5, 2010, 11:22pm UTC](https://www.ruby-forum.com/t/define-a-string-as-a-model-object/182984/6 "2010-02-05T23:22:54Z")

</div>

Frank\_in\_Tennessee wrote:

> Works great now. Thank you.
> 
> Modified Code:
> 
> model\_name = “User”  
> @Model = model\_name.constantize  
> @Model.find(params[:id]).name

Great. One other thing: @Model is unusual. You might consider @model  
instead as being more idiomatic Ruby.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
