Mongoid:返回embedded_in类

时间:2011-08-23 11:38:30

标签: ruby-on-rails ruby mongodb mongoid

如何获取嵌入我的实例的对象类的名称:

class Person
  include Mongoid::Document
  embeds_many :addresses
end

class Address
  include Mongoid::Document
  embedded_in :person
end

my_instance = Person.first.adresses.first
my_instance.embedded?
#=> true
my_instance.embedded_class????? # <=== I need to get this
#=> Person

1 个答案:

答案 0 :(得分:3)

您有两种选择:

my_instance.metadata.inverse_class_name.constantize
# or use the undocumented _parent method
my_instance._parent.class