@employee.address.build
I found them difficult to get working as well. In general I have found
the "dirty data only" "optimization" a bit too agressive. I was able
to get embedded documents and nested forms to work reliable as follows
(YMMV):
1. monkey patched mongoid w/ an "init_embedded" method
Module Mongoid
module Document
def init_embedded(field, validate=false)
return unless send(field).nil?
klass = options[:klass]||field.classify.constantize
obj = klass.new
send("#{field}=", obj)
obj.save(validate) unless new_record?
end
end
end
2. In a before filter on my controller call init_embedded on each
embedded class (recursively).
class AppController < ApplicationController
before_filter :init_embedded
# ...
def init_embedded
@app.init_embedded('user_info')
@app.user_info.init_embedded('address')
return true
end
end
3. assign each embedded document from the params separately before
saving
class AppController < ApplicationController
def save
@app.attributes=params[:app])
@app.user_info.attributes=params[:app][:user_info]
@app.save
end
end
This worked for me, it may not be the "correct" way but it was the
only way I could get the data to stick.
rick