Update bypasses validation

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Works as Designed
    • Priority: Major - P3
    • None
    • Affects Version/s: 7.3.5, 7.4.3, 8.0.6, 8.1.2, 7.5.4
    • Component/s: Attributes, Validations
    • None
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Let's say I have an application with a has_and_belongs_to_many relation between a Client model and a Company model.

      In the Client model there is a validation to make sure the object is associated with at least one company (companies cannot be blank).

      The thing is, when I try to update a client with an empty list of companies, despite raising a validation error message, the invalid model is updated in the database.

      Is this an expected behavior?

      One can reproduce it by running the following code:

      class Company
        include Mongoid::Document
      
        field :name, type: String
      end
      
      class Client
        include Mongoid::Document
      
        field :name, type: String
      
        has_and_belongs_to_many :companies, class_name: 'Company'
      
        validates :companies, presence: true
      end
      
      c = Client.first
      c.update!(companies: []) # raises error but updates document
      

            Assignee:
            Jamis Buck
            Reporter:
            Alex Bevilacqua
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: