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

查看所有标签

猜你喜欢:

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

ActionScript 3.0 Cookbook

ActionScript 3.0 Cookbook

Joey Lott、Darron Schall、Keith Peters / Adobe Dev Library / 2006-10-11 / GBP 28.50

Well before Ajax and Microsoft's Windows Presentation Foundation hit the scene, Macromedia offered the first method for building web pages with the responsiveness and functionality of desktop programs......一起来看看 《ActionScript 3.0 Cookbook》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

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

Base64 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具