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

validates uniqueness of a field with scope not working

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.1
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Hi,
      validates_uniqueness_of :title, :scope => where(posted_on:

      { "$gt" => posted_on.beginning_of_day, "$lte" => posted_on.end_of_day})
      is not working. Being title is a string field and posted_on is a datetime field. Actual problem is validate uniqueness of title in a day.

      If found an alternate solution.
      validates_uniqueness_of :title, if: -> { Ad.where(title: title, posted_on: { "$gt" => posted_on.beginning_of_day, "$lte" => posted_on.end_of_day}

      ).size > 0 }

      Can you suggest a better solution.

            Assignee:
            dianna.hohensee@mongodb.com Dianna Hohensee (Inactive)
            Reporter:
            aashish.kiran@gmail.com aashish kiran
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: