如何使用Mongoid更新Rails中的嵌套Mongo文档属性

如何使用Mongoid更新Rails中的嵌套Mongo文档属性

本文介绍了如何使用Mongoid更新Rails中的嵌套Mongo文档属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(如果这个问题缺少详细信息,请提前道歉,我会观察评论并添加我能提供的信息)

(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

在文档document_template的内部,有以下references_many,我想对其进行修改.具体来说,我想更改引用的字体:

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.

以下是使用Rails控制台快速显示问题的信息:

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.

使用mongo(而不是导轨中的mongoid)更新对象可以正常工作.

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

Kyle Banker要求提供一些日志记录信息,所以就在这里.不幸的是,我找不到比Rails服务器的输出更好的日志记录源,这似乎表明从未进行过更新调用.对于某些上下文,这是来自控制器的一些信息:

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

这是处理POST时来自Rails服务器的输出:

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

似乎完全没有用于更新font_ids的MONGODB命令...

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

推荐答案

我倾向于相信 Mongoid问题#357 导致了此问题.上面的日志记录表明,该产品的document_template.fonts(或font_ids)的更新似乎与错误描述相匹配.

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.

(旁注:如果不是:stored_as =>:array,我有点困惑font_ids数组的确切来源.我不确定100%应该修改但是因为font_ids是一个数组,而fonts是一个Mongoid :: Criteria,所以阻力最小的路径是font_ids.)

(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.)

从数据的角度来看,:field和:embeds_ *似乎很相似,因为信息嵌入在父文档中.

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

这篇关于如何使用Mongoid更新Rails中的嵌套Mongo文档属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:32