obtaining a model name

Dear user community,

I know that I can access the controller that is called by using
params[:controller] . I need to obtain the model name for a find
command and I was wondering if there was a built in way of obtaining
this name. If I can't obtain it I have to ways to obtain it, by using
the controller and modding the string as I go through it or creating
a database and find the association in the database.

I would much rather use a built in variable or method to obtain this

Any and all help is appreciated


What exactly do you want to do with the model 'name'. I believe you
already know your model class name. And if you are talking about
getting reference of the model object in your controller then you have
the option of creating sessions or using a global hash.

Please throw in more details about your requirement.


Is it inside of a model? If so, you could likely do something like...


...or whatever find command you're looking for.

If that's not what you need, then we need more details, please. :slight_smile:


You can do something like this.

case1: Array of object returned by your query like this
            x = Product.find(:all) # may what ever query you want.
           model_name = x.first.class
           puts model_name # outputs Product
case2: Your query returns only single object
            x = Product.find(:first) # may what ever query you want.
           model_name = x.class
           puts model_name # outputs Product