从mongoid标准获取类名

时间:2013-03-22 11:24:49

标签: ruby-on-rails ruby ruby-on-rails-3 mongoid

irb(main):019:0> a = Admin.first
=> #<Admin _id: 514b318da61654dd05000002, email: "email@email.com", encrypted_password: "$2a$10$b.cWtfPNgUcCubJRX2vjAO6vEsds9OTGO2tn6Lfz2cBU.mwTYsHw.">

irb(main):020:0> a.class.name
=> "Admin"

irb(main):021:0> a = Admin.all
=> #<Mongoid::Criteria
  selector: {}
  options:  {}
  class:    Admin
  embedded: false>

irb(main):022:0> a.class.name
=> "Mongoid::Criteria"

我如何在“Mongoid :: Criteria”类中获得模型名称

谢谢!

1 个答案:

答案 0 :(得分:3)

你可以这样做:

a.klass   #=> Admin
or
a.klass.name #=> "Admin"