如何获取嵌入我的实例的对象类的名称:
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
答案 0 :(得分:3)
您有两种选择:
my_instance.metadata.inverse_class_name.constantize
# or use the undocumented _parent method
my_instance._parent.class