相关数据未保存

时间:2014-02-15 15:09:52

标签: ruby-on-rails mongodb mongoid

有一个简单的模型Customer与Order的关系为1:n。

class Customer 
  include Mongoid::Document

  field :name, type: String
  has_many :orders

end

这段代码,

customer = Customer.new name: "John"
customer.orders.build item: "ipad"  #=> doesn't work in the method create
customer.save                       #=> true

在rails4控制台上工作但在create方法中不起作用,其中只保存客户名称,但顺序不是,并且好像构建方法不存在。因此,项目“ipad”不会保存,也不会发生任何订单。

我正在使用rails(4.0.2)+ mongoid(4.0.0.alpha1)。

知道什么是根本原因?

1 个答案:

答案 0 :(得分:1)

通过http://mongoid.org/en/mongoid/docs/relations.html

class Band
  include Mongoid::Document
  has_many :albums, autosave: true
end

band = Band.first
band.albums.build(name: "101")
band.save #=> Will save the album as well.

干杯!

相关问题