How to update nested attributes of a Mongo document in Rails using Mongoid?

(I apologize in advance if this question is unclear in details, I will look at 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 are the following many_many links that I want to change. In particular, 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 logic and details should I have in my controller and form in order to do this? Please comment if you want me to add some of the crazy things I've tried; however, I was not lucky with any of them.

The following is a brief overview of the problem using the rails console:

# Grab a Product and check how many fonts are in it 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 (rather than mongoid on rails) works as expected.

, . , , rails, , , . :

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

rails POST:

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

, MONGODB font_ids ...

+3
2

, Mongoid Issue # 357. , document_template.fonts( font_ids), .

(sidenote: , font_ids , : array_as = > : array. 100% , , font_ids :: , - font_ids.)

: field : embages_ * , .

+1

. - . , , .

MongoDB, ?

0

Source: https://habr.com/ru/post/1779507/


All Articles