KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Given an arbitrary mongoid document how do i convert it to JSON and include any embedded structures without specifically including those structures in my to_json statement. For example: #!/usr/bin/env ruby require 'mongoid' require 'json' require 'pp' class Doc include Mongoid::Document include Mongoid::Timestamps field :doc_specific_info , type: String embeds_many :persons end class Person include Mongoid::Document field :role , type: String field :full_name , type: String embeds_many :addresses embedded_in :Doc end class Address include Mongoid::Document field :full_address , type: String end doc = Doc.new doc.doc_specific_info = "TestReport" p = Person.new p.role = 'buyer' p.full_name = 'JOHN DOE' doc.persons << p a = Address.new a.full_address = '1234 nowhere ville' doc.persons.first.addresses << a # THIS STATEMENT pp JSON.parse(doc.to_json(:include => { :persons => { :include => :addresses } } ) ) # GIVES ME # {"_id"=>"4ee0d30fab1b5c5743000001", # "created_at"=>nil, # "doc_specific_info"=>"TestReport", # "updated_at"=>nil, # "persons"=> # [{"_id"=>"4ee0d30fab1b5c5743000002", # "full_name"=>"JOHN DOE", # "role"=>"buyer", # "addresses"=> # [{"_id"=>"4ee0d30fab1b5c5743000003", # "full_address"=>"1234 nowhere ville"}]}]} # THIS STATEMENT pp JSON.parse(doc.to_json() ) # GIVES ME # {"_id"=>"4ee0d2f8ab1b5c573f000001", # "created_at"=>nil, # "doc_specific_info"=>"TestReport", # "updated_at"=>nil} So what I want is a statement something like this: # FOR A STATEMENT LIKE THIS pp JSON.parse(doc.to_json( :everything } ) ) # TO GIVE ME THE COMPLETE DOCUMENT LIKE SO: # {"_id"=>"4ee0d30fab1b5c5743000001", # "created_at"=>nil, # "doc_specific_info"=>"TestReport", #
Tags (comma-separated)
Save Edits
Cancel