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

Association proxies method_missing does not handle keyword args

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 7.3.2, 7.2.5, 7.1.10
    • Affects Version/s: None
    • Component/s: None
    • None

      Using mongoid 7.3.1 and ruby 3.0.2 the following causes ArgumentError exceptions to be raised:

       

      class User
        include Mongoid::Documentfield :name, type: String
        has_many :stuff, dependent: :restrict_with_error
      
        def foo(my_keyword_arg: 'foo')
          puts "#{my_keyword_arg}!"
        end
      end
      
      class Stuff
        include Mongoid::Documentfield :foo, type: String
        belongs_to :user
        
        def foo_my_user
          user.foo(my_keyword_arg: 'bar')
        end
      end
      
      user = User.create!(name: 'my user')
      stuff = Stuff.create!(foo: 'bar', user: user)
      stuff.user.foo # => outputs 'foo!' as expected
      stuff.user.foo(my_keyword_arg: 'baz') # => raises ArgumentError
      stuff.foo_my_user # => raises ArgumentError

       

      The cause seems to be that Association::Proxy#method_missing is not handling keyword arguments correctly; instead of

      ```def method_missing(name, *args, &block)```

      you'll probably want

      ```def method_missing(name, *args, **kwargs, &block)```

       

      `#respond_to_missing` below will probably need that treatment as well.

       

            Assignee:
            dmitry.rybakov@mongodb.com Dmitry Rybakov
            Reporter:
            sven.riedel@givve.com Sven Riedel
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: