9

I have a model defined like this :

class Foo
  include ::Mongoid::Document

  field :name, type: String
  field :followed_bars, type: Array
  field :favorite_bars, type: Array
end

I created a Foo object like this :

foo = Foo.new(name: "Test")
foo.save

In my DB when I type db.foo.find() I can see the object I just created. Then, in my application I'm trying to do this :

foo = Foo.first
foo.push(:followed_bars, "hello")

And every time I'm getting an error : ArgumentError: wrong number of arguments (2 for 1)

I'm not sure to understand what am I missing here ?

Thanks in advance for help !

Regards.

4

1 回答 1

23

我刚刚找到了如何对 mongoid 数组进行推送。

在 API 文档中,他们给出了一个示例(mongoid 3.x):

Model#push    person.push(:aliases, "007")

我使用的是 mongoid 4.0.0,他们改变了方法定义,现在我们必须使用新的语法,所以我不得不写:

foo.push(aliases: "test")

问题就这样解决了。

于 2013-09-08T20:09:14.083 回答