Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3417

validates_presence_of on embeds_many documents removing embeds when validation fails using update_attributes

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.2
    • Affects Version/s: None
    • Component/s: None
    • None

      When validates_presence_of embeds_many documents fails on update_atrtibutes a mongo "pullAll" command is executed and embeds_many is deleted. Example:

      class Ho
        include Mongoid::Document
      
        embeds_many :heys
        field :name, type: String
        validates_presence_of :heys
      end
      
      class Hey
        include Mongoid::Document
      
        embedded_in :ho
        field :name, type: String
      end
      
      ho = Ho.new
      ho.heys = [Hey.new]
      ho.save!
      ho.heys
      >> [#<Hey _id: 5295387669a951f9a4000004, name: nil>]
      
      ho.update_attributes!({"heys"=>nil})
      >> MOPED: 127.0.0.1:27017 UPDATE  database=database_name collection=hos selector={"_id"=>"529537f669a951f9a4000002"} update={"$pullAll"=>{"heys"=>[{"_id"=>"5295387669a951f9a4000004"}]}} flags=[] (0.2277ms)
      >> Mongoid::Errors::Validations: 
      Problem:
        Validation of Ho failed.
      Summary:
        The following errors were found: Heys translation missing: pt-BR.mongoid.errors.models.ho.attributes.heys.blank
      Resolution:
      
      ho.reload
      ho.heys
      >> []
      

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            alangalvino Alan Gomes Alvino
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: