3

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?

bulleric
  • 2,077
  • 6
  • 21
  • 36
  • Possible duplicate of [Limit number of objects in has\_many association](http://stackoverflow.com/questions/2263267/limit-number-of-objects-in-has-many-association) – hjpotter92 May 08 '17 at 09:12

1 Answers1

5

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.

Community
  • 1
  • 1
muffinista
  • 6,676
  • 2
  • 30
  • 23