Hito
Hito

Reputation: 707

pushing item in mongoid array

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.

Upvotes: 9

Views: 6231

Answers (1)

Hito
Hito

Reputation: 707

I just found how to do a push on a mongoid array.

In the API documentation they give an example (mongoid 3.x) :

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

I'm using mongoid 4.0.0 and they changed the method definition, now we have to use the new syntax so I had to write :

foo.push(aliases: "test")

Problem solved so.

Upvotes: 23

Related Questions