# Dymanic changes for model name

**URL:** <https://www.ruby-forum.com/t/dymanic-changes-for-model-name/116890>\
**Category:** Rails\
**Created:** [October 10, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/dymanic-changes-for-model-name/116890 "2007-10-10T21:58:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![demoversion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@demoversion](https://www.ruby-forum.com/u/demoversion)\
**Post date:** [October 10, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/dymanic-changes-for-model-name/116890/1 "2007-10-10T21:58:47Z")

</div>

Hello,

I have a question. But first take a look on this code:

**Attachments** \<%= view\_attachments\_field(Garage.find(@partial\_object\_id), {}) %\>

This code loading images for model Garage in my view by id. This is a  
partial. And i used it in several places but for different models :City,  
Shop, House…

I just don’t want to write code by Ctrl+Ins,Shift+Ins and do this  
dynamically. I want to set model name in some controller and render this  
partial for any model that support attachments.

I want something like this

**Attachments** \<%= view\_attachments\_field(\<\>.find(@partial\_object\_id), {}) %\>

How to do it?

Thanks

---

<div class="post-metadata">

**Author:** ![demoversion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@demoversion](https://www.ruby-forum.com/u/demoversion)\
**Post date:** [October 11, 2007, 8:57am UTC](https://www.ruby-forum.com/t/dymanic-changes-for-model-name/116890/2 "2007-10-11T08:57:34Z")

</div>

Who knows? 🙂

---

<div class="post-metadata">

**Author:** ![demoversion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@demoversion](https://www.ruby-forum.com/u/demoversion)\
**Post date:** [October 11, 2007, 10:48am UTC](https://www.ruby-forum.com/t/dymanic-changes-for-model-name/116890/3 "2007-10-11T10:48:38Z")

</div>

Maybe this is a solution?

class A  
def self.hello  
puts “hello A”  
end  
end

class B  
def self.hello  
puts “hello B”  
end  
end

A.hello  
B.hello

name1 = “A”  
name2 = “B”

eval “#{name1}.hello”  
eval “#{name2}.hello”

---

<div class="post-metadata">

**Author:** ![demoversion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@demoversion](https://www.ruby-forum.com/u/demoversion)\
**Post date:** [October 11, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/dymanic-changes-for-model-name/116890/4 "2007-10-11T17:43:48Z")

</div>

On 10/10/07, Igor K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How to do it?  
> Well, you can just pass the model class from your controller to the  
> partial through an instance variable or (better) through :locals. But  
> why not fetch the row in your controller and pass the actual object to  
> the partial instead of it’s class and id?  
> Maybe I’m missing something…

---

<div class="post-metadata">

**Author:** ![demoversion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@demoversion](https://www.ruby-forum.com/u/demoversion)\
**Post date:** [October 11, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/dymanic-changes-for-model-name/116890/5 "2007-10-11T13:15:24Z")

</div>

> name1 = “A”  
> name2 = “B”
> 
> ## eval “#{name1}.hello” eval “#{name2}.hello”

Better: name1.constantize

Fred
