Note that there are some explanatory texts on larger screens.

plurals
  1. POHow Do I Update Nested Mongo Document Attributes in Rails with Mongoid?
    text
    copied!<p>(Apologies in advance if this question is short on details, I'll watch the comments and add what I can)</p> <p>I have a Model with the following:</p> <pre><code>class Product include Mongoid::Document include Mongoid::Timestamps #... field :document_template, :type =&gt; Document accepts_nested_attributes_for :document_template </code></pre> <p>Inside the Document document_template, is the following references_many, which I want to modify. Specifically, I want to change which fonts are referenced:</p> <pre><code>class Document include Mongoid::Document include Mongoid::Timestamps #... references_many :fonts, :stored_as =&gt; :array, :inverse_of =&gt; :documents </code></pre> <p>What sort of logic and details should I have in my controller and form to get this done? Please comment if you would like me to add some of the zany things I've tried; however, I haven't had any luck with any of them.</p> <p>Here is a quick showing of the issue using rails console:</p> <pre><code># Grab a Product and check how many fonts are in it's document_template ruby-1.8.7-p302 &gt; prod = Product.find(:first) =&gt; ... ruby-1.8.7-p302 &gt; prod._id =&gt; BSON::ObjectId('4d06af15afb3182bf5000111') ruby-1.8.7-p302 &gt; prod.document_template.font_ids.count =&gt; 9 # Remove a font from the font_ids array ruby-1.8.7-p302 &gt; prod.document_template.font_ids.pop =&gt; BSON::ObjectId('...') # This font id was removed from font_ids ruby-1.8.7-p302 &gt; prod.document_template.font_ids.count =&gt; 8 # Save the changes ruby-1.8.7-p302 &gt; prod.document_template.save! =&gt; true ruby-1.8.7-p302 &gt; prod.save! =&gt; true # Instantiate a new product object of that same product ruby-1.8.7-p302 &gt; prod_new = Product.find(:first) =&gt; ... # Confirm the _ids are the same ruby-1.8.7-p302 &gt; prod._id == prod_new._id =&gt; true # Check to see if the changes were persisted ruby-1.8.7-p302 &gt; prod_new.document_template.font_ids.count =&gt; 9 # If the changes persisted, this should be 8. # Grrrrr... doesn't look like it. Will the change disappear after a reload too? ruby-1.8.7-p302 &gt; prod.reload =&gt; ... ruby-1.8.7-p302 &gt; prod.document_template.font_ids.count =&gt; 9 # ಠ_ಠ ... no dice. </code></pre> <p>Updating objects using mongo (and not mongoid in rails) works as expected.</p> <p>Kyle Banker has asked for some logging info, so here it is. Unfortunatly, I couldn't find a better source of logging than the output from rails server, which seems to suggest the update call is never being made. For some context here is some info from the controller:</p> <pre><code>def update_resource(object, attributes) update_pricing_scheme(object, attributes) update_document_template_fonts(object, attributes) end def update_document_template_fonts(object, attributes) document_template = object.document_template document_template_attributes = attributes[:document_template_attributes] font_ids = document_template_attributes[:font_ids] font_ids.delete("") # Removing an empty string that tags along with the font_ids. font_ids.collect! { |f| BSON::ObjectId(f) } # Mongo want BSON::ObjectId object.document_template.font_ids.replace font_ids object.document_template.save!(:validate =&gt; false) object.save!(:validate =&gt; false) end </code></pre> <p>Here is the output from rails server when the POST is processed:</p> <pre><code>Started GET "/admin/products/4d091b18afb3180f3d000111" for 127.0.0.1 at Wed Dec 15 13:57:28 -0600 2010 Started POST "/admin/products/4d091b18afb3180f3d000111" for 127.0.0.1 at Wed Dec 15 13:57:49 -0600 2010 Processing by Admin::ProductsController#update as HTML Parameters: {"commit"=&gt;"Update Product", "authenticity_token"=&gt;"QUW0GZw7nz83joj8ncPTtcuqHpHRtp1liq8fB7/rB5s=", "utf8"=&gt;"✓", "id"=&gt;"4d091b18afb3180f3d000111", "product"=&gt;{"name"=&gt;"Ho Ho Ho Flat Multiple Photo Modern Holiday Card", "document_template_attributes"=&gt;{"id"=&gt;"4d091b18afb3180f3d000112", "font_ids"=&gt;["", "4d091b17afb3180f3d000023"]}, "description"=&gt;"", "pricing_scheme_id"=&gt;"4d091b17afb3180f3d00003b"}} development['users'].find({:_id=&gt;BSON::ObjectId('4d091b17afb3180f3d00009b')}, {}).limit(-1) development['products'].find({:_id=&gt;BSON::ObjectId('4d091b18afb3180f3d000111')}, {}).limit(-1) development['pricing_schemes'].find({:_id=&gt;BSON::ObjectId('4d091b17afb3180f3d00003b')}, {}).limit(-1) MONGODB development['products'].update({"_id"=&gt;BSON::ObjectId('4d091b18afb3180f3d000111')}, {"$set"=&gt;{"updated_at"=&gt;Wed Dec 15 19:57:50 UTC 2010}}) in Document#set_default_color_scheme: self.color_scheme = #&lt;ColorScheme:0xb52f6f38&gt; MONGODB development['documents'].update({"_id"=&gt;BSON::ObjectId('4d091b18afb3180f3d000112')}, {"$set"=&gt;{"color_scheme_name"=&gt;"green_charcoal_black", "updated_at"=&gt;Wed Dec 15 19:57:50 UTC 2010}}) Redirected to http://localhost:3000/admin/products/4d091b18afb3180f3d000111 Completed 302 Found in 49ms </code></pre> <p>It looks like the MONGODB command to update the font_ids is completely absent...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload