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

validates_uniqueness_of not working

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      I have tried many different version of mongoid and I have never seen validates_uniqueness_of :field work for me. Does this work as advertised in the docs?
      I am now on the latest version of mongoid (beta 19 ) and it still doesn't work, here is a sample script showing it doesn't work:

      require 'rubygems'
      require 'mongoid'

      Mongoid.configure do |config|
      name = "unique_test"
      host = "localhost"
      config.master = Mongo::Connection.new.db(name)

      config.persist_in_safe_mode = false
      end

      class User
      include Mongoid::Document
      embeds_many :posts
      field :login
      end

      class Post
      include Mongoid::Document
      field :data
      field :sid, :type => Integer
      embedded_in :user, :inverse_of => :posts
      validates_uniqueness_of :sid
      end

      sid = 21088517421

      u = User.create(:login => 'me')
      u.posts << Post.new(:sid => sid,:data => 'data')
      u.posts << Post.new(:sid => sid,:data => 'data')
      u.save
      u.reload
      puts u.posts.size == 2 #WHY is this true?
      puts u.posts.inspect
      u.destroy

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

              Created:
              Updated:
              Resolved: