Forum: Ruby on Rails get model name

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.
Mohamed S. (Guest)
on 2007-07-13 04:25
hello

how to get model name for specific table inside code?

i.e.

table: people --> model: person

how to get this?
thanks
Daniel -. (Guest)
on 2007-07-13 04:32
(Received via mailing list)
On 7/13/07, Mohamed S. <removed_email_address@domain.invalid> wrote:
> how to get this?
> thanks
>
> --



If you have an instance of person

 "#{@person.class}".downcase

will get you a string in lowercase of the class name.

HTH
Daniel
Rob B. (Guest)
on 2007-07-13 06:20
(Received via mailing list)
On Jul 12, 2007, at 8:25 PM, Mohamed S. wrote:

> thanks
Don't ask that here.
   Ask there =>  http://nubyonrails.com/tools/pluralize

You want:
   "people".classify => "Person"
You might also want:
  "people".classify.constantize => Person

constantize gives you the class, not a String.

-Rob

Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
Matthew Rudy J. (Guest)
on 2007-07-13 16:10
Of course,
all of these rely on using the default table name,

if out of legacy you have a class

class CrazyClass
    set_table_name :this_is_crazy
end

this wouldn't work.

Rob B. wrote:
> On Jul 12, 2007, at 8:25 PM, Mohamed S. wrote:
>
>> thanks
> Don't ask that here.
>    Ask there =>  http://nubyonrails.com/tools/pluralize
>
> You want:
>    "people".classify => "Person"
> You might also want:
>   "people".classify.constantize => Person
>
> constantize gives you the class, not a String.
>
> -Rob
>
> Rob B.    http://agileconsultingllc.com
> removed_email_address@domain.invalid
This topic is locked and can not be replied to.