title: Mongoid, confused with embedded document with timestamps and versioning?
body: <p>I have been using Mongoid for about 3 months now, and I have managed to get done pretty much anything I need thanks to the great document and resources out there.</p>
  
  <p>But going back to improve some stuff I have made a few backs, I am definitely struggling a lot on embedded documents.</p>
  
  <p>In a nutshell what I am trying to do, is to maintain versioning and timestamps on embedded documents, but that I cannot manage to do.</p>
  
  <p>Here is the relevant part of my model:</p>
  
  <pre><code>class Content
    include Mongoid::Document
    include Mongoid::Timestamps
    include Mongoid::Paranoia
  
    embeds_many :localized_contents
    accepts_nested_attributes_for :localized_contents
  end
  
  class LocalizedContent
    include Mongoid::Document
    include Mongoid::Timestamps
    include Mongoid::Paranoia
    include Mongoid::Versioning
  
    embedded_in :content, :inverse_of =&gt; :localized_contents
  end
  </code></pre>
  
  <p>Nothing really complicated here, everything works fine regarding the behavior of the Content model, however the <code>LocalizedContent</code> model is not behaving the way I am expecting to, so my expectations either needs to get straighten up, or I need help fixing what I am doing wrong.</p>
  
  <p>To create a new embedded document I do the following:</p>
  
  <pre><code>my_content = Content.find(params[:id])
  my_content.localized_contents.build(params[:localized_content])
  if parent.save
   #redirect, etc.
  end
  </code></pre>
  
  <p>This works in the sense that it successfully creates a new embedded document in the correct Content, however the timestamps fields I left a nil</p>
  
  <p>Now, if I try to update that localized_content:</p>
  
  <pre><code>my_content = Content.find(params[:content_id])
  localized_content = my_content.localized_contents.find(params[:id])
  </code></pre>
  
  <p>Now, if I do: <code>localized_content.update_attributes(params[:localized_content])</code> I get the following error:</p>
  
  <pre><code>=&gt; Mongoid::Errors::InvalidCollection: Access to the collection for LocalizedContent is not allowed since it is an embedded document, please access a collection from the root document.
  </code></pre>
  
  <p>Fair enough, then I update atomically the fields on the localized content and save the parent:</p>
  
  <pre><code>localized_content.fieldA = "value"
  localized_content.fieldB = "value"
  localized_content.fieldC = "value"
  
  my_content.save
  </code></pre>
  
  <p>This works in updating the localized content properly but:
  - timesteamps (udpated_at and created_at) are still nil
  - versions does not receive the a copy of the current localized_content and version does not get incremented !</p>
  
  <p>So as I read in many occasion in this groups and on some forums on the web, the call backs are not triggered on the embedded document for performance reason, since I am calling save on the parent. Again, faire enough, but as suggested in those places, I should call save on the embedded docs instead... but how !?!?! because every time I do I get the dreaded:</p>
  
  <pre><code>=&gt; Mongoid::Errors::InvalidCollection: Access to the collection for LocalizedContent is not allowed since it is an embedded document, please access a collection from the root document.
  </code></pre>
  
  <p>Even more so, I tried to manually call the call back for versioning on my embedded: localized_content.revise, and again same error:</p>
  
  <pre><code>=&gt; Mongoid::Errors::InvalidCollection: Access to the collection for LocalizedContent is not allowed since it is an embedded document, please access a collection from the root document.
  </code></pre>
  
  <p>I am going nuts here ! Please help. What I am doing wrong ? How should an embedded document be create and updated so I can call (even manually I don't care) the proper callbacks to update the time stamps and versioning ?</p>
  
  <p>Thanks,</p>
  
  <p>Alex</p>
  
  <p>ps: I am using rails 3.0.3 and mongoid 2.0.1</p>