Limit number of objects in has_many association with mongoid

眉间皱痕 提交于 2019-12-10 10:25:33

问题


I want to limit the number of associated objects in a has_many association between a post and pictures :

In active record i can do something like

class post < < ActiveRecord::Base
  has_many :pictures, :limit => 2
end

But mongoid raise an exception with limit:

Invalid option :limit provided to relation :pictures. Valid options are: as, autosave, dependent, foreign_key, order, class_name, extend, inverse_class_name, inverse_of, name, relation, validate. (Mongoid::Errors::InvalidOptions)

Is there an alternative implementation to relize :limit?


回答1:


using has_many :limit doesn't actually restrict the number of objects in the association -- it just limits the number of results returned when querying the relation. You probably want something like this answer Limit number of objects in has_many association -- which will show you how to check the number of assocated objects during validation.



来源:https://stackoverflow.com/questions/10817914/limit-number-of-objects-in-has-many-association-with-mongoid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!