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

Add :conditions option example to reference documentation

    • Fully Compatible

      One can use :conditions option to validates_uniqueness_of to limit the scope of validation:

                  Dictionary.validates_uniqueness_of(:name,
                    conditions: -> { Dictionary.where(:year.gte => 1900, :year.lt => 2000) })
      

      This allows dictionary to be created with years 950 and 1950:

                    Dictionary.create!(name: "French-English", year: 950)
                    Dictionary.create!(name: "French-English", year: 1950)
      

      ... but not the other way around:

                    Dictionary.create!(name: "French-English", year: 1950)
                    Dictionary.create!(name: "French-English", year: 950)
      

      One might expect the specified conditions to be applied to the model instance being validated, but this is not the case: conditions only apply to the database query. For avoidance of confusion, this behavior should be documented.

      AR works the same way as Mongoid.

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: