Forum: Ruby on Rails Is it possible to pass a model name as a variable?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Shanmu G. (Guest)
on 2009-01-21 09:14
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
Andrew T. (Guest)
on 2009-01-21 09:40
(Received via mailing list)
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/.
>
> >
>


--
Andrew T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
Andrew T. (Guest)
on 2009-01-21 09:42
(Received via mailing list)
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/.
>
> >
>
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://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
Shanmu G. (Guest)
on 2009-01-21 15:17
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/.
>>
>> >
>>
> 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://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
This topic is locked and can not be replied to.