问题
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