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.
2366b35e3045fbee354edcbce654656b?d=identicon&s=25 Mohamed Saeed (mohamedsaeed)
on 2007-07-13 02:25
hello

how to get model name for specific table inside code?

i.e.

table: people --> model: person

how to get this?
thanks
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-07-13 02:32
(Received via mailing list)
On 7/13/07, Mohamed Saeed <rails-mailing-list@andreas-s.net> 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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-07-13 04:20
(Received via mailing list)
On Jul 12, 2007, at 8:25 PM, Mohamed Saeed 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
E60b2dc57668b5662ce3f07781e41710?d=identicon&s=25 Matthew Rudy Jacobs (matthewrudy)
on 2007-07-13 14: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 Biedenharn wrote:
> On Jul 12, 2007, at 8:25 PM, Mohamed Saeed 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 Biedenharn    http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.