Mongoid中class_name和inverse_class_name有什么区别?

时间:2013-05-13 11:38:10

标签: mongoid mongoid3

Mongoid中class_name和inverse_class_name有什么区别?

1 个答案:

答案 0 :(得分:1)

此代码将让您清楚地了解差异:

class Order
  include Mongoid::Document
  embeds_many :items
end

class Item
  include Mongoid::Document
  embedded_in :order
end

item = Item.new
order = Order.create({ items: [ item ] })

puts order.items.metadata.class_name
# => Item

puts order.items.metadata.inverse_class_name
# => Order

也就是说,class_name是关系的referrer类,而inverse_class_name是关系的裁判类。