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

Localized fields uniqueness validator chokes on blank value

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

      Relatively minor issue, but in my model I have a localized field with a uniqueness validator.

          field :name, type: String, localized: true
          validates_uniqueness_of :name
      

      Normally it will query for uniqueness with the following:

      `"$or"=>[{"name.en"=>"My name"}]`
      

      But if the input value is empty, it queries with:

      `"$or"=>[]`
      

      Which will trigger an error from the DB:

      2015-04-29 08:34:14.177 FATAL 
      Moped::Errors::QueryFailure - The operation: #<Moped::Protocol::Query
        @length=154
        @request_id=165
        @response_to=0
        @op_code=2004
        @flags=[]
        @full_collection_name="my_database.shops"
        @skip=0
        @limit=1
        @selector={"$or"=>[], :_id=>{"$ne"=>"5411425b6f295806f5001cdf"}}
        @fields={:_id=>1}>
      failed with error 17287: "Can't canonicalize query: BadValue $and/$or/$nor must be a nonempty array"
      
      See https://github.com/mongodb/mongo/blob/master/docs/errors.md
      for details about this error.:
        moped (1.5.2) lib/moped/node.rb:384:in `block in query'
        moped (1.5.2) lib/moped/node.rb:589:in `block (3 levels) in flush'
        moped (1.5.2) lib/moped/node.rb:588:in `block (2 levels) in flush'
        moped (1.5.2) lib/moped/node.rb:150:in `ensure_connected'
        moped (1.5.2) lib/moped/node.rb:584:in `block in flush'
        moped (1.5.2) lib/moped/node.rb:599:in `logging'
        moped (1.5.2) lib/moped/node.rb:583:in `flush'
        moped (1.5.2) lib/moped/node.rb:572:in `process'
        moped (1.5.2) lib/moped/node.rb:372:in `query'
        moped (1.5.2) lib/moped/cursor.rb:138:in `block in load_docs'
        moped (1.5.2) lib/moped/session/context.rb:109:in `block in with_node'
        moped (1.5.2) lib/moped/cluster.rb:209:in `block in with_primary'
        moped (1.5.2) lib/moped/node.rb:197:in `ensure_primary'
        moped (1.5.2) lib/moped/cluster.rb:208:in `with_primary'
        moped (1.5.2) lib/moped/session/context.rb:108:in `with_node'
        moped (1.5.2) lib/moped/cursor.rb:137:in `load_docs'
        moped (1.5.2) lib/moped/cursor.rb:25:in `each'
        moped (1.5.2) lib/moped/query.rb:76:in `each'
        mongoid (3.1.6) lib/mongoid/contextual/mongo.rb:423:in `check_existence'
        mongoid (3.1.6) lib/mongoid/contextual/mongo.rb:146:in `exists?'
        C:0:in `exists?'
        mongoid (3.1.6) lib/mongoid/validations/uniqueness.rb:273:in `validate_root'
        mongoid (3.1.6) lib/mongoid/validations/uniqueness.rb:52:in `block in validate_each'
        mongoid (3.1.6) lib/mongoid/validations/queryable.rb:23:in `with_query'
        mongoid (3.1.6) lib/mongoid/validations/uniqueness.rb:46:in `validate_each'
        activemodel (3.2.20) lib/active_model/validator.rb:153:in `block in validate'
        activemodel (3.2.20) lib/active_model/validator.rb:150:in `validate'
        activesupport (3.2.20) lib/active_support/callbacks.rb:310:in `_callback_before_23394'
        activesupport (3.2.20) lib/active_support/callbacks.rb:814:in `_run__43917789__validate__1737515264__callbacks'
        activesupport (3.2.20) lib/active_support/callbacks.rb:405:in `__run_callback'
        activesupport (3.2.20) lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
        activesupport (3.2.20) lib/active_support/callbacks.rb:81:in `run_callbacks'
        mongoid (3.1.6) lib/mongoid/callbacks.rb:130:in `run_callbacks'
        activemodel (3.2.20) lib/active_model/validations.rb:228:in `run_validations!'
        activemodel (3.2.20) lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
        activesupport (3.2.20) lib/active_support/callbacks.rb:535:in `_run__43917789__validation__1737515264__callbacks'
        activesupport (3.2.20) lib/active_support/callbacks.rb:405:in `__run_callback'
        activesupport (3.2.20) lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
        activesupport (3.2.20) lib/active_support/callbacks.rb:81:in `run_callbacks'
        mongoid (3.1.6) lib/mongoid/callbacks.rb:130:in `run_callbacks'
        activemodel (3.2.20) lib/active_model/validations/callbacks.rb:53:in `run_validations!'
        activemodel (3.2.20) lib/active_model/validations.rb:195:in `valid?'
        mongoid (3.1.6) lib/mongoid/validations.rb:84:in `valid?'
        activemodel (3.2.20) lib/active_model/validations.rb:203:in `invalid?'
        mongoid (3.1.6) lib/mongoid/persistence/modification.rb:22:in `prepare'
        mongoid (3.1.6) lib/mongoid/persistence/operations/update.rb:45:in `persist'
        mongoid (3.1.6) lib/mongoid/persistence.rb:150:in `update'
        mongoid (3.1.6) lib/mongoid/persistence.rb:87:in `save'
        mongoid (3.1.6) lib/mongoid/persistence.rb:186:in `update_attributes'
        my_route/shops_controller.rb:36:in `update'
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            johnnyshields Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: