【问题标题】:Mongoid relationship gives "wrong number of arguments (1 for 2)" errorMongoid 关系给出“错误数量的参数(1 对 2)”错误
【发布时间】:2013-06-28 04:50:16
【问题描述】:

我正在 Sinatra 中从 mongo_mapper 迁移到 mongoid,当与自定义关系名称存在关系时,我在测试中不断收到“参数数量错误(1 对 2)”错误,我真的卡住了。

我有以下设置:

型号

class Idea
  include Mongoid::Document
  include Mongoid::Timestamps

  field :text, type: String

  belongs_to :author, class_name: "User"
end

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name, type: String

  has_many :ideas
end

工厂

Factory.define :idea do |f|
  f.sequence(:text) { |n| "Idea #{n}" }
  f.association :author, :factory => :user
end

Factory.define :user do |f|
  f.sequence(:name) { |n| "Name #{n}" }
end

规格

describe "Something" do
  before do
    Factory(:idea) # <-- This fails
  end

  # ...
end

我在做傻事吗?遗漏了什么?我尝试使用inverse_of,但这似乎也没有解决它。等效的在 mongo_mapper 中工作正常,所以我觉得这是我在 mongoid 中做错的一些语法事情。

【问题讨论】:

    标签: ruby mongodb rspec sinatra mongoid


    【解决方案1】:

    原来是我愚蠢。我以前有一个 hack 方法 User#ideas,这与 mongoid 连接事物的方式相冲突(出于某种原因,它在 mongo_mapper 中需要)

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      相关资源
      最近更新 更多