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

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

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.0.0
    • Component/s: None
    • Labels:
    • Sprint:
      Ruby Sprint 35

      Description

      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?

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved: