# Is it possible to pass a model name as a variable?

**URL:** <https://www.ruby-forum.com/t/is-it-possible-to-pass-a-model-name-as-a-variable/158263>\
**Category:** Rails\
**Created:** [January 21, 2009, 8:14am UTC](https://www.ruby-forum.com/t/is-it-possible-to-pass-a-model-name-as-a-variable/158263 "2009-01-21T08:14:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shanmu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@shanmu](https://www.ruby-forum.com/u/shanmu)\
**Post date:** [January 21, 2009, 8:14am UTC](https://www.ruby-forum.com/t/is-it-possible-to-pass-a-model-name-as-a-variable/158263/1 "2009-01-21T08:14:03Z")

</div>

Hi all,  
Is it possible to pass a model name as a variable?  
For example in controller there is a method like this,

def list  
var = User.find(:all)  
end

where in var we heve collected all data from users table. If i edit the  
above code like the one below, will it do the same work?

def list(modl)  
var = tab.find(:all)  
end

def hi  
tab = User  
list(tab)  
end

someone please help.

thanks,  
shanmu

---

<div class="post-metadata">

**Author:** ![shanmu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@shanmu](https://www.ruby-forum.com/u/shanmu)\
**Post date:** [January 21, 2009, 8:40am UTC](https://www.ruby-forum.com/t/is-it-possible-to-pass-a-model-name-as-a-variable/158263/2 "2009-01-21T08:40:30Z")

</div>

Yes it will but you have a bug in your code, it should be:

def list(modl)

On Wed, Jan 21, 2009 at 9:14 AM, Shanmu G. \<  
removed\_email\_address@domain.invalid\> wrote:

> above code like the one below, will it do the same work?
> 
> someone please help.
> 
> ## thanks, shanmu
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![shanmu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@shanmu](https://www.ruby-forum.com/u/shanmu)\
**Post date:** [January 21, 2009, 2:17pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-pass-a-model-name-as-a-variable/158263/3 "2009-01-21T14:17:23Z")

</div>

Andrew T. wrote:

> On Wed, Jan 21, 2009 at 9:14 AM, Shanmu G. \<  
> removed\_email\_address@domain.invalid\> wrote:
> 
> > above code like the one below, will it do the same work?
> > 
> > someone please help.
> > 
> > ## thanks, shanmu
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> Yes it will but you have a bug in your code, it should be:
> 
> def list(modl)  
> var = modl.find(:all) #Must call find on modl, not tab  
> end
> 
> def hi  
> tab = User  
> list(tab)  
> end
> 
> Can be simplified as:  
> def list(modl)  
> modl.find(:all)  
> end
> 
> def hi  
> list(User)  
> end
> 
> –  
> Andrew T.  
> [http://ramblingsonrails.com](http://ramblingsonrails.com)  
> [http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)
> 
> “I have never let my schooling interfere with my education” - Mark Twain

Hi andrews thanks for ur reply. its working. thanks for ur optimization  
too

regards,  
shanmu

---

<div class="post-metadata">

**Author:** ![shanmu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@shanmu](https://www.ruby-forum.com/u/shanmu)\
**Post date:** [January 21, 2009, 8:42am UTC](https://www.ruby-forum.com/t/is-it-possible-to-pass-a-model-name-as-a-variable/158263/4 "2009-01-21T08:42:15Z")

</div>

On Wed, Jan 21, 2009 at 9:14 AM, Shanmu G. \<  
removed\_email\_address@domain.invalid\> wrote:

> above code like the one below, will it do the same work?
> 
> someone please help.
> 
> ## thanks, shanmu
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

Yes it will but you have a bug in your code, it should be:

def list(modl)  
var = modl.find(:all) #Must call find on modl, not tab  
end

def hi  
tab = User  
list(tab)  
end

Can be simplified as:  
def list(modl)  
modl.find(:all)  
end

def hi  
list(User)  
end

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain
