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

validate_associated adds wrong error messages to parent document

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Validations
    • None
    • 3

      I am on mongoid 4.0.0.beta1, rails 4.0.2 and ruby 2.1

      I have got

          class User
            include Mongoid::Document
      
            embeds_one :courier, class_name: "Users::Courier"
            validates_associated :courier
            accepts_nested_attributes_for :courier
          end
      

      and

          module Users
            class Courier
              include Mongoid::Document
          
              embedded_in :user
      
              field :gender,     :type => String
              field :firstname,  :type => String
              field :lastname,   :type => String
      
              validates :gender, presence: true, inclusion: { in: ["female", "male"] }
              validates :firstname, presence: true
              validates :lastname, presence: true
            end
          end
      

      I also have a nested form:

          = form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
              ....
              = f.fields_for :courier do |f|
                  ... fields for embedded model
      

      when i try to create a new user using this form and omit firstname and lastname of the embedded courier i get the following error object:

          #<ActiveModel::Errors:0x007f4cc1ba7ce0 @base=#<User _id: 530388c2476f6c6ca8000000, email: "eqfef@eefe.de", encrypted_password: "$2a$10$21loiHytt0d4V95/Mj5P1.ol8dSrKc7xzP9g3ynWoZN4Mf7DRexyy", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, unconfirmed_email: nil>, @messages={:courier=>["ist nicht gültig", "ist nicht gültig"]}>
      

      whereas the errors on courier are:

          #<ActiveModel::Errors:0x007f4cc1bb3ce8 @base=#<Users::Courier _id: 530388d3476f6c6ca8010000, gender: "female", firstname: "", lastname: "">, @messages={:firstname=>["muss ausgefüllt werden"], :lastname=>["muss ausgefüllt werden"]}>
      

      Shouldn't user's errors contain those of courier? At least this part is strange:

      {:courier=>["ist nicht gültig", "ist nicht gültig"]}

      note:
      "muss ausgefüllt werden" is german for "can't be blank"
      and "ist nicht gültig" is "is invalid"

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            Goltergaul Goltergaul [X]
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: