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

first_or_create doesn't pass validation when used with a block

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 3.1.4
    • Component/s: None
    • Labels:

      Description

      mongoid-3.1.3, email and password fields in the following examples are required.

      user = User.where(email: 'user1@example.com').first_or_create do |u|
      u.password = '12345678'
      end

      Expected user.persisted? to be true
      Got false

      Raising validation error when using with a bang:

      Mongoid::Errors::Validations:
      Problem:
      Validation of Referrer failed.
      Summary:
      The following errors were found: Password can't be blank
      Resolution:
      Try persisting the document with valid data or remove the validations.

      While the following works as expected:

      r = User.create do |u|
      u.email = 'user1@example.com'
      u.password = '12345678'
      end

      User.where(email: 'user1@example.com').first_or_create(password: '12345678')

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            yevgenko yevgenko
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: