Question

(Apologies in advance if this question is short on details, I'll watch the comments and add what I can)

I have a Model with the following:

class Product
  include Mongoid::Document
  include Mongoid::Timestamps
#...
  field :document_template, :type => Document
  accepts_nested_attributes_for :document_template

Inside the Document document_template, is the following references_many, which I want to modify. Specifically, I want to change which fonts are referenced:

class Document
  include Mongoid::Document
  include Mongoid::Timestamps
#...
  references_many :fonts, :stored_as => :array, :inverse_of => :documents

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.

Here is a quick showing of the issue using rails console:

# Grab a Product and check how many fonts are in it's document_template
ruby-1.8.7-p302 > prod = Product.find(:first)
 => ...
ruby-1.8.7-p302 > prod._id
 => BSON::ObjectId('4d06af15afb3182bf5000111') 
ruby-1.8.7-p302 > prod.document_template.font_ids.count
 => 9

# Remove a font from the font_ids array
ruby-1.8.7-p302 > prod.document_template.font_ids.pop
 => BSON::ObjectId('...')    # This font id was removed from font_ids
ruby-1.8.7-p302 > prod.document_template.font_ids.count
 => 8

# Save the changes
ruby-1.8.7-p302 > prod.document_template.save!
 => true 
ruby-1.8.7-p302 > prod.save!
 => true 

# Instantiate a new product object of that same product
ruby-1.8.7-p302 > prod_new = Product.find(:first)
 => ...

# Confirm the _ids are the same
ruby-1.8.7-p302 > prod._id == prod_new._id
 => true 

# Check to see if the changes were persisted
ruby-1.8.7-p302 > prod_new.document_template.font_ids.count
 => 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 > prod.reload
 => ...
ruby-1.8.7-p302 > prod.document_template.font_ids.count
 => 9

# ಠ_ಠ ... no dice.

Updating objects using mongo (and not mongoid in rails) works as expected.

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:

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 => false)
  object.save!(:validate => false)
end

Here is the output from rails server when the POST is processed:

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"=>"Update Product", "authenticity_token"=>"QUW0GZw7nz83joj8ncPTtcuqHpHRtp1liq8fB7/rB5s=", "utf8"=>"✓", "id"=>"4d091b18afb3180f3d000111", "product"=>{"name"=>"Ho Ho Ho Flat Multiple Photo Modern Holiday Card", "document_template_attributes"=>{"id"=>"4d091b18afb3180f3d000112", "font_ids"=>["", "4d091b17afb3180f3d000023"]}, "description"=>"", "pricing_scheme_id"=>"4d091b17afb3180f3d00003b"}}

development['users'].find({:_id=>BSON::ObjectId('4d091b17afb3180f3d00009b')}, {}).limit(-1)
development['products'].find({:_id=>BSON::ObjectId('4d091b18afb3180f3d000111')}, {}).limit(-1)
development['pricing_schemes'].find({:_id=>BSON::ObjectId('4d091b17afb3180f3d00003b')}, {}).limit(-1)

MONGODB development['products'].update({"_id"=>BSON::ObjectId('4d091b18afb3180f3d000111')}, {"$set"=>{"updated_at"=>Wed Dec 15 19:57:50 UTC 2010}})

in Document#set_default_color_scheme: self.color_scheme = #<ColorScheme:0xb52f6f38>

MONGODB development['documents'].update({"_id"=>BSON::ObjectId('4d091b18afb3180f3d000112')}, {"$set"=>{"color_scheme_name"=>"green_charcoal_black", "updated_at"=>Wed Dec 15 19:57:50 UTC 2010}})

Redirected to http://localhost:3000/admin/products/4d091b18afb3180f3d000111
Completed 302 Found in 49ms

It looks like the MONGODB command to update the font_ids is completely absent...

Was it helpful?

Solution

I'm partial to believing Mongoid Issue #357 is causing the problem. The logging above suggests that an update for the product's document_template.fonts (or font_ids) which seems to match the bug description.

(sidenote: I'm a bit confused where exactly the font_ids array comes from if not the :stored_as => :array. I'm not 100% certain which I should be modifying either but since font_ids is an Array and fonts is a Mongoid::Criteria, the path of least resistance is font_ids.)

From a data standpoint, :field and :embeds_* seem to be similar in that the information is embedded in the parent document.

OTHER TIPS

Mongoid has a complicated back end. Therefore, the easiest way to diagnose this is to enable the driver's logging. Then we can look at the exact messages being sent to the database in both cases, and we're sure to get an answer.

Can you attach a logger when you connect to MongoDB and then post the relevant sections of log output?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top