If given Model.find(:all), how to get the name of the Model/Class from the given result?
m = Model.find(1);
m.class_name would give you "Model"
If we have:
开发者_运维技巧m = Model.find(:all);
How do we get the name of the model from m alone?
Get the class of the first entry in the returned array
m.first.class
If you mean, how do you aggregate them all, since you are actually returning an array of Model objects, I recommend this:
Model.find(:all).collect(&:model_name)
This should give you an array of model names of the classes that you have returned from the database.
If you are calling "Model", don't you already know the class?
If you call
Post.find(:all)
the returned records will be of class Post.
精彩评论