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

issue of UniquenessValidator for embedded document

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

      <pre><code>
      class Dict
      include Mongoid::Document
      embeds_many :keys
      end

      class Key
      include Mongoid::Document
      include Mongoid::Timestamps

      field :name
      key :name

      validates_uniqueness_of :name
      embedded_in :dict, :inverse_of => :keys
      end
      </code></pre>

      no issue if I comment "key :name" in Key class. But if I enable it, the unique validator will fail.

      In code UniquenessValidator line 39, the criteria fails to work because _id is the attribute. It will also return false when invoking exists? method later.
      criteria = criteria.where(attribute => unique_search_value(value), :_id =>

      {'$ne' => document._id}

      )

      Remove the second condition, the unique works fine.
      criteria = criteria.where(attribute => unique_search_value(value))

      another issue:

      If I use key method on name, I thought the name should be unique by default? but it is not in fact. Not sure the reason.

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

              Created:
              Updated:
              Resolved: