开发者

If given Model.find(:all), how to get the name of the Model/Class from the given result?

开发者 https://www.devze.com 2022-12-18 07:36 出处:网络
m = Model.find(1); m.class_name would give you \"Model\" If we have: 开发者_运维技巧m = Model.find(:all);
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.

0

精彩评论

暂无评论...
验证码 换一张
取 消