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

Uniqueness validation for StringifiedSymbol field in an embedded document fails

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 7.4.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Minor Change

      Uniqueness validation for StringifiedSymbol field in embedded document doesn't work.

      The following test fails with Mongoid 7.3.3:

      class Band
        include Mongoid::Document
        field :name, type: Mongoid::StringifiedSymbol
        validates_uniqueness_of :name
        embeds_many :albums
      end
      
      class Album
        include Mongoid::Document
        field :name, type: Mongoid::StringifiedSymbol
        validates_uniqueness_of :name
        embedded_in :band
      end
      
      describe "Band with multiple albums with same name" do
        it "should be invalid" do
          band = Band.new
          band.albums.build(name: :foo)
          band.albums.build(name: :foo)
          expect(band.invalid?).to be true # not ok
        end
      end
      

      The test passes if the type of the field is Symbol.

      Validation in root document works:

      describe "Band with duplicate name" do
        it "should be invalid" do
          _band = Band.create(name: :foo)
          expect(Band.new(name: :foo).invalid?).to be true # ok
        end
      end
      

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            kaz@qualified.io Kaz Yoshihara
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: