canoe
canoe

Reputation: 1293

associated data is not saved

There are a simple model Customer which has a relation with Order as 1:n.

class Customer 
  include Mongoid::Document

  field :name, type: String
  has_many :orders

end

This piece of code,

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

works on a rails4 console but doesn't work in the method create, where only the customer name is saved, but the order isn't and as if the build method is not there. As a result, the item "ipad" is not saved and no any orders happens.

I'm using rails(4.0.2) + mongoid(4.0.0.alpha1).

Any idea what's root cause?

Upvotes: 0

Views: 44

Answers (1)

CodeGroover
CodeGroover

Reputation: 2187

via 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.

Cheers!

Upvotes: 1

Related Questions