我正在使用Mongoid和Rails 3.什麼是限制可以存儲在每個父對象(專輯)中的嵌入對象(照片)的數量的最佳方式是什麼?Mongoid:限制嵌入對象的數量
class Album
include Mongoid::Document
embeds_many :photos
end
class Photo
include Mongoid::Document
embedded_in :album, :inverse_of => :photos
end
用ActiveRecord,我會做這樣的事情:
has_many :photos, :before_add => :enforce_photo_limit
private
def enforce_photo_limit
raise "Too many photos" if self.photos.count >= 50
end
...但是這不Mongoid支持。
任何建議非常感謝。
謝謝。