Uniqueness constraint violated when attribute doesn't change but scope changes.

XMLWordPrintableJSON

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

      class Folder
        include Mongoid::Document
        field :name, :type => String
        has_many :folder_items
      end
      
      class FolderItem
        include Mongoid::Document
        belongs_to :folder
        field :name, :type => String
        validates :name, :uniqueness => {:scope => :folder_id} 
      end
      
      personal_folder.folder_items << FolderItem.new(:name => "non-unique") 
      public_folder.folder_items   << FolderItem.new(:name => "non-unique") 
      
      public_folder.folder_items.first.update_attributes(:folder_id => personal_folder.id)
      

      Last statement should fail, but it doesn't. It's because as per the MONGOID-1577 we skip db call if the attribute value hasn't changed. However this skips the fact that value may remain the same but scope may change; hence the failure.

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

                Created:
                Updated:
                Resolved: