Clicking an item in a mongoid array

I have a model defined as follows:

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 as follows:

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

In my DB, when I enter db.foo.find (), I can see the newly created object. Then in my application, I try to do this:

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

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

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

Thanks in advance for your help!

Sincerely.

+6
source share
1 answer

I just found how to push to a mongoid array.

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

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

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

 foo.push(aliases: "test") 

The problem is solved like this.

+18
source

Source: https://habr.com/ru/post/953427/


All Articles