(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:
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
ruby-1.8.7-p302 > prod.document_template.font_ids.pop
=> BSON::ObjectId('...')
ruby-1.8.7-p302 > prod.document_template.font_ids.count
=> 8
ruby-1.8.7-p302 > prod.document_template.save!
=> true
ruby-1.8.7-p302 > prod.save!
=> true
ruby-1.8.7-p302 > prod_new = Product.find(:first)
=> ...
ruby-1.8.7-p302 > prod._id == prod_new._id
=> true
ruby-1.8.7-p302 > prod_new.document_template.font_ids.count
=> 9
ruby-1.8.7-p302 > prod.reload
=> ...
ruby-1.8.7-p302 > prod.document_template.font_ids.count
=> 9
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("")
font_ids.collect! { |f| BSON::ObjectId(f) }
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
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
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:
Completed 302 Found in 49ms
, MONGODB font_ids ...