# Using a string to find a model name

**URL:** <https://www.ruby-forum.com/t/using-a-string-to-find-a-model-name/73652>\
**Category:** Rails\
**Created:** [October 25, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-model-name/73652 "2006-10-25T23:20:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [October 25, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-model-name/73652/1 "2006-10-25T23:20:48Z")

</div>

Hi Rails,

I have a string in my controller that I want to make into a model name.  
EG:

Class LlamaController \< ApplicationController  
def addsomething  
model = params[:model\_name]  
# Here, I would like to take the string in model and use it as a  
Model  
@foo = Model.new  
end  
end

Is this possible? Is there a better way?

Thanks  
–Dean

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [October 25, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-model-name/73652/2 "2006-10-25T23:28:49Z")

</div>

> end
> 
> Is this possible? Is there a better way?

@foo = Class.const\_get(model).new

I think.

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [October 25, 2006, 11:34pm UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-model-name/73652/3 "2006-10-25T23:34:49Z")

</div>

Cool, thanks.

0 urzatron app/models % …/…/script/console  
Loading development environment.

> > Class.const\_get(‘User’).new

=\> #\<User:0xf7151cd0 @attributes={“created\_on”=\>nil, “updated\_on”=\>nil,  
“password”=\>"", “login”=\>"", “css\_style”=\>“default”}, @new\_record=true\>

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [October 26, 2006, 12:10am UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-model-name/73652/4 "2006-10-26T00:10:52Z")

</div>

On Wednesday 25 October 2006 23:17, Dean wrote:

> ```
> @foo = Model.new
> 
> ```
> 
> end  
> end
> 
> Is this possible? Is there a better way?

model.constantize

or

model.camelize.constantize

Inflector magic…

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
