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

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

内容简介:翻译自: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数组中的项目》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

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

Data Structures and Algorithms in Java

Data Structures and Algorithms in Java

Robert Lafore / Sams / 2002-11-06 / USD 64.99

Data Structures and Algorithms in Java, Second Edition is designed to be easy to read and understand although the topic itself is complicated. Algorithms are the procedures that software programs use......一起来看看 《Data Structures and Algorithms in Java》 这本书的介绍吧!

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

在线压缩/解压 HTML 代码

html转js在线工具
html转js在线工具

html转js在线工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具