ruby-on-rails – 推送mongoid数组中的项目

栏目: Ruby · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/18683148/pushing-item-in-mongoid-array

我有一个这样定义的模型:

class Foo
  include ::Mongoid::Document

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

我创建了一个像这样的Foo对象:

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

在我的数据库中输入db.foo.find()时,我可以看到刚刚创建的对象.

然后,在我的应用程序中,我正在尝试这样做:

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

每次我收到错误:ArgumentError:错误的参数个数(2个为1)

我不确定我在这里错过了什么?

在此先感谢您的帮助!

问候.

我刚刚发现了如何对mongoid数组进行推送.

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

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

我正在使用mongoid 4.0.0并且他们改变了方法定义,现在我们必须使用新语法,所以我必须写:

foo.push(aliases: "test")

问题解决了.

翻译自:https://stackoverflow.com/questions/18683148/pushing-item-in-mongoid-array


以上所述就是小编给大家介绍的《ruby-on-rails – 推送mongoid数组中的项目》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Shallows

The Shallows

Nicholas Carr / W. W. Norton & Company / 2010-6-15 / USD 26.95

"Is Google making us stupid?" When Nicholas Carr posed that question, in a celebrated Atlantic Monthly cover story, he tapped into a well of anxiety about how the Internet is changing us. He also crys......一起来看看 《The Shallows》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

随机密码生成器
随机密码生成器

多种字符组合密码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码