首页 > 解决方案 > MongoID 为任何异常抛出错误数量的参数。Ruby 3.0.0 + Rails 6.1.3.1

问题描述

我的堆栈是

Ruby 3.0.0 Rails 6.1.3.1 Mongoid 7.1.2

Mongo 在应该引发其他异常时总是抛出相同的异常:

ArgumentError (wrong number of arguments (given 3, expected 1..2))

例如:

我对字段进行了唯一性验证:

irb(main):010:0> Model.create!(field: 'sape')
=> #<Model _id: 606207343dcde26a1595eb48, created_at: 2021-03-29 16:58:28.006091 UTC, updated_at: 2021-03-29 16:58:28.006091 UTC, field: "sape">

irb(main):011:0> Model.create!(field: 'sape')
Traceback (most recent call last):
        1: from (irb):11:in `<main>'
ArgumentError (wrong number of arguments (given 3, expected 1..2))

find_by 也一样:如果响应找到一个,它会检索它:

irb(main):012:0> Model.find_by(field: 'sape')
=> #<Model _id: 606207343dcde26a1595eb48, created_at: 2021-03-29 16:58:28.006 UTC, updated_at: 2021-03-29 16:58:28.006 UTC, field: "sape">

但如果它不存在:

irb(main):001:0> Model.find_by(field: 'sape')
Traceback (most recent call last):
        1: from (irb):1:in `<main>'
ArgumentError (wrong number of arguments (given 2, expected 0..1))

这是我的模型:

class Model
  include ActiveModel::Validations
  include Mongoid::Document
  include Mongoid::Timestamps::Created
  include Mongoid::Timestamps::Updated

  field :field, type: String, default: nil

  index({ field: 1 }, { unique: true })
end

标签: ruby-on-railsmongoid

解决方案


Ruby 3 目前不受 Mongoid 支持,不应使用。

您可以在此处查看哪些 Rubies Mongoid 支持。


推荐阅读