Best way to update embedded document in mongoid
I migrate my project from mysql to m开发者_开发问答ongodb and have some troubles.
How to update attributes in embedded document in ActiveRecord way? It is possible in mongoid?
class Template
include Mongoid::Document
include Mongoid::Versioning
embedded_in :site, :inverse_of => :templates
end
class Site
embeds_many :templates
end
For example:
site = Site.find(params[:current_site_id])
template = site.templates.find(params[:id])
template.update_attributes(params[:template])
I got:
Access to the collection for Template is not allowed since it is an embedded document, please access a collection from the root document.
Ok, but how to do it from the root document?
Edit:
It looks like Mongoid::Versioning which I use in Template make some troubles. I remove included module and everything works fine.
There is a pb with mongoid's versioning with embedded documents, same goes if you are using paranoia mode. I would suggested deactivating those options for your embedded docs and replicating the functionality yourself if you really need it.
Alex
精彩评论