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

Matchable false when trying using lt,lte,gt,gte on float and string

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 6.0.0
    • Affects Version/s: None
    • Component/s: None

      If we do

      class Test
        include Mongoid::Document
        field :foo
      end
      
      Test.create foo: 3
      Test.where({a: 3}).first.matches?({ a: { '$lt' => '3.1' }})
      

      If the type of value for foo is provided by a 3rd party and therefore unknown this is a perfectly valid question. However we get "ArgumentError: comparison of Fixnum with String failed". When the result should be false.

      I am currently getting around this with the following monkey patch:

      # encoding: utf-8
      module Mongoid
        module Matchable
          class Default
      
            protected
      
            def determine(value, operator)
              attribute.__array__.any? {|attr|
                begin
                  attr ? attr.send(operator, first(value)) : false
                rescue ArgumentError
                  false
                end
              }
            end
      
          end
        end
      end
      

      Happy to write up tests and provide a PR, but wanted to open for discussion first. IE does rescue ArgumentError catch anything that it shouldn't?

            Assignee:
            Unassigned Unassigned
            Reporter:
            msaspence Matthew Spence
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: