我想在mongoid中創建一個embeds_many關係,它似乎沒有對我添加到集合的對象強制執行類型約束。我在這裏做了些什麼不正確的事情還是這個設計?Mongoid embeds_many類型約束
class Item
include Mongoid::Document
embeds_many :extra_fields, class_name: "ItemType"
end
class ItemType
include Mongoid::Document
embedded_in :item
field :type_name
end
class Category
include Mongoid::Document
embeds_many :schema_fields
field :rabble
end
ruby > Item.delete_all
=> 1
ruby > item = Item.new
=> #<Item _id: 4e20a60a401b3e47d2000004, _type: nil>
ruby > item.extra_fields << Category.new(:rabble => 'derp')
=> [#<Category _id: 4e20a619401b3e47d2000005, _type: nil, rabble: "derp">]
ruby > item.save
=> true
ruby > item = Item.first
=> #<Item _id: 4e20a60a401b3e47d2000004, _type: nil>
ruby > item.extra_fields[0].rabble
=> "derp"
是啊,這是可能的力量。我發現Mongo Mapper確實在某種程度上強制執行了這些關聯約束,所以現在就玩這個。 – Mynock