Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongoid 7.1.0 issue with arguments #85

Closed
SimonVillage opened this issue Apr 3, 2020 · 9 comments · Fixed by #86
Closed

mongoid 7.1.0 issue with arguments #85

SimonVillage opened this issue Apr 3, 2020 · 9 comments · Fixed by #86

Comments

@SimonVillage
Copy link

something stopped working with mongoid 7.1.0 gem release. It was still fine with 7.1.0.rc0

irb(main):001:0> Address.last
Traceback (most recent call last):
        4: from (irb):1
        3: from app/models/address.rb:3:in `<main>'
        2: from app/models/address.rb:8:in `<class:Address>'
        1: from app/models/address.rb:8:in `include'
ArgumentError (wrong number of arguments (given 7, expected 1))

address.rb

# frozen_string_literal: true

class Address
  # == Inclusions ===========================================================

  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Locker
@dblock
Copy link
Collaborator

dblock commented Apr 3, 2020

Maybe Ruby version-related (named arguments?)? Either way try to PR something?

@dblock dblock added the bug? label Apr 3, 2020
@SimonVillage
Copy link
Author

I will debug more tomorrow. It only happens when include Mongoid::Locker is included. And also only when upgrading from mongoid 7.1.0.rc0 to 7.1.0. We would have to check what changes they made from rc0 to 7.1.0

@SimonVillage
Copy link
Author

SimonVillage commented Apr 4, 2020

I am struggling finding the issue:

I created a new application

rails new mongoid-locker-test --skip-active-record --skip-bundle --api

added the following gems

bundle add mongoid mongoid-locker

added a test model

class Test

  include Mongoid::Document
  include Mongoid::Locker

  field :address, type: String

end

getting the same error

irb(main):002:0> Test
Traceback (most recent call last):
        5: from (irb):1
        4: from (irb):2:in `rescue in irb_binding'
        3: from app/models/test.rb:3:in `<main>'
        2: from app/models/test.rb:7:in `<class:Test>'
        1: from app/models/test.rb:7:in `include'
ArgumentError (wrong number of arguments (given 7, expected 1))

repository is here: https://github.com/SimonHausdorf/mongoid-locker-test

Edit:
mongoid-locker 1.0.1 works with newest mongoid

@dks17
Copy link
Collaborator

dks17 commented Apr 4, 2020

Hello.

Mongoid gem doesn't use ActiveSupport method delegate since 7.1 version. Fixed it #86

But we still have one test failure, Mongoid::Criteria query chain works differently for 7.1 version and <=7.0 versions:

7.1

model.where(_id: 1)
=> #<Mongoid::Criteria
  selector: {"_id"=>1}
  options:  {}
  class:    User
  embedded: false>

model.where(_id: 1).locked
=> #<Mongoid::Criteria
  selector: {"_id"=>1, "$and"=>[{"locking_name"=>{:$exists=>true, :$ne=>nil}}, {"locked_at"=>{:$exists=>true, :$ne=>nil}}, {"$where"=>"new Date() - this.locked_at < 5000"}]}
  options:  {}
  class:    User
  embedded: false>

model.where(_id: 1).locked.where(_id: 2)
=> #<Mongoid::Criteria
  selector: {"_id"=>1, "$and"=>[{"_id"=>2}]}
  options:  {}
  class:    User
  embedded: false>

7.0

model.where(_id: 1)
=> #<Mongoid::Criteria
  selector: {"_id"=>1}
  options:  {}
  class:    User
  embedded: false>

model.where(_id: 1).locked
=> #<Mongoid::Criteria
  selector: {"_id"=>1, "$and"=>[{"locking_name"=>{:$exists=>true, :$ne=>nil}}, {"locked_at"=>{:$exists=>true, :$ne=>nil}}, {"$where"=>"new Date() - this.locked_at < 5000"}]}
  options:  {}
  class:    User
  embedded: false>

model.where(_id: 1).locked.where(_id: 2)
=> #<Mongoid::Criteria
  selector: {"_id"=>2, "$and"=>[{"locking_name"=>{:$exists=>true, :$ne=>nil}}, {"locked_at"=>{:$exists=>true, :$ne=>nil}}, {"$where"=>"new Date() - this.locked_at < 5000"}]}
  options:  {}
  class:    User
  embedded: false>

We can make this test loose, but I thought put the log here.

So, is this the gem issue?

@SimonVillage
Copy link
Author

Hey :)

@dks17
Copy link
Collaborator

dks17 commented Apr 4, 2020

@SimonHausdorf began write and pressed ctrl+enter to go to new line. I have finished the comment.

@dks17
Copy link
Collaborator

dks17 commented Apr 4, 2020

@SimonHausdorf this is known issue, see workarounds here

@dks17
Copy link
Collaborator

dks17 commented Apr 4, 2020

About strange querying behavior, something similar
Strange querying behavior in mongoid v7.0.5
Useless $and when calling Foo.and(Foo.or(...))

@dks17 dks17 closed this as completed in #86 Apr 5, 2020
Cruikshanks added a commit to DEFRA/waste-carriers-engine that referenced this issue Jun 18, 2020
The front-office and back-office apps when the engine has been bumped have also taken on the latest version of mongoid-locker (v2.0.1) because it was not pinned.

Though only a minor change it appears it has a dependency on Mongoid v7 and changes it has made around not using ActiveSupport method `delegate` since version 7.1

We ascertained this from a statement made in the issue [mongoid 7.1.0 issue with arguments](mongoid/mongoid-locker#85 (comment))

> Mongoid gem doesn't use ActiveSupport method delegate since 7.1 version.

The issue was fixed in the mongoid-locker PR [Add forwardable module and upgrade rubocop](mongoid/mongoid-locker#86). And that change seems to be the key difference between version 2.0.0 and 2.0.1.

The error we see in the apps when v2.0.1 is being used is

```
An error occurred while loading ./spec/forms/waste_carriers_engine/base_forms_spec.rb.
Failure/Error: delegate :contact_address, :contact_email, :registered_address, to: :registration
ArgumentError:
  wrong number of arguments (given 4, expected 1)
```

In this example if you go to order_copy_cards_registration.rb:10 you'll find

```ruby
    delegate :contact_address, :contact_email, :registered_address, to: :registration
```

Depending on whether you run rspec or try to hit a page in the app this is the error that gets thrown.

So we are pinning the version of mongoid-locker we use, until we can complete the work to upgrade the version of ruby and rails we are using. We should then be able to use Mongoid v7.1 (or greater) and hopefully the issue goes away!
Cruikshanks added a commit to DEFRA/waste-carriers-engine that referenced this issue Jun 18, 2020
* Bump mongoid-locker from 2.0.0 to 2.0.1

Bumps [mongoid-locker](https://github.com/mongoid/mongoid-locker) from 2.0.0 to 2.0.1.
- [Release notes](https://github.com/mongoid/mongoid-locker/releases)
- [Changelog](https://github.com/mongoid/mongoid-locker/blob/master/CHANGELOG.md)
- [Commits](mongoid/mongoid-locker@v2.0.0...v2.0.1)

Signed-off-by: dependabot-preview[bot] <[email protected]>

* Pin mongoid-locker to 2.0.0

The front-office and back-office apps when the engine has been bumped have also taken on the latest version of mongoid-locker (v2.0.1) because it was not pinned.

Though only a minor change it appears it has a dependency on Mongoid v7 and changes it has made around not using ActiveSupport method `delegate` since version 7.1

We ascertained this from a statement made in the issue [mongoid 7.1.0 issue with arguments](mongoid/mongoid-locker#85 (comment))

> Mongoid gem doesn't use ActiveSupport method delegate since 7.1 version.

The issue was fixed in the mongoid-locker PR [Add forwardable module and upgrade rubocop](mongoid/mongoid-locker#86). And that change seems to be the key difference between version 2.0.0 and 2.0.1.

The error we see in the apps when v2.0.1 is being used is

```
An error occurred while loading ./spec/forms/waste_carriers_engine/base_forms_spec.rb.
Failure/Error: delegate :contact_address, :contact_email, :registered_address, to: :registration
ArgumentError:
  wrong number of arguments (given 4, expected 1)
```

In this example if you go to order_copy_cards_registration.rb:10 you'll find

```ruby
    delegate :contact_address, :contact_email, :registered_address, to: :registration
```

Depending on whether you run rspec or try to hit a page in the app this is the error that gets thrown.

So we are pinning the version of mongoid-locker we use, until we can complete the work to upgrade the version of ruby and rails we are using. We should then be able to use Mongoid v7.1 (or greater) and hopefully the issue goes away!

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Alan Cruikshanks <[email protected]>
@phstc
Copy link

phstc commented Sep 22, 2020

👋 I'm also getting this error with mongoid 6.4.5 and mongoid-locker 2.0.1. I couldn't find any workaround to make it work.

SomeModel.first

ArgumentError: wrong number of arguments (given 3, expected 1)
from /Users/pablo.cantero/.asdf/installs/ruby/2.5.3/lib/ruby/2.5.0/forwardable.rb:132:in `instance_delegate'
    mongoid (6.4.5)
    mongoid-locker (2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants