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

Save with validate: false in conjunction with an association sets errors on the model

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.2
    • Affects Version/s: None
    • Component/s: None

      Consider the following example

      class Organization
        include Mongoid::Document
      
        field :name
        validates_presence_of :name
      
        has_many :members
      end
      
      class Member
        include Mongoid::Document
      
        belongs_to :organization
        
        field :name
        field :email
        validates_presence_of :organization, :name, :email
      end
      
      org = Organization.create!(name: 'Pipewise')
      m = Member.new(organization: org, email: 'admin@pipewise.com')
      m.save(validate: false) # <= returns true as expected
      m.errors.present? # <= returns true, while expected false
      

      This presents a problem for controllers using the Responder. The Responder looks at the model errors to decide whether to return a happy response or 422. In this case we get 422 even though the model has been persisted.

            Assignee:
            Unassigned Unassigned
            Reporter:
            alexeits Alex Tsibulya
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: