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

validates_uniqueness_of embedded_document with key

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.3.4
    • Affects Version/s: None
    • Component/s: None

      class Seller
        include Mongoid::Document
        field :seller_id, :type => Integer
        field :name, :type => String
        embeds_many :seller_products
        key :seller_id
      end
      
      class SellerProduct
        include Mongoid::Document
        field :product_id, :type => Integer
        key :product_id
        embedded_in :seller, :inverse_of => :seller_products
        validates_uniqueness_of :product_id
      end
      

      `

      Validates_uniqueness_of doesnt work if i set my key to be :product_id

      Uniqueness validator criteria becomes:
      <Mongoid::Criteria
      selector: {:product_id=>10, :_id=>{"$ne"=>"10"}},
      options: {},
      class: SellerProduct,
      embedded: true>

      When i remove the key it works

      <Mongoid::Criteria

      selector: {:product_id=>1, :_id=>{"$ne"=>BSON::ObjectId('4eaf0733a5d09fb9ef000006')}},
      options: {},
      class: SellerProduct,
      embedded: true>

            Assignee:
            Unassigned Unassigned
            Reporter:
            danielnc daniel naves de carvalho
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: