Including nested objects in JSON response, from MongoMapper objects
Asked Answered
P

2

8
class Api::StoresController < ApplicationController  
  respond_to :json

  def index
    @stores = Store.all(:include => :products)
    respond_with @stores
  end
end

Returns only stores without their products, as does

Store.find(:all).to_json(:include => :products)

The association is tested, I can see the nested products in console ouput from, say,

Store.first.products

What's the correct way to get them products included with MongoMapper?

Here are my models:

   class Store
     include MongoMapper::Document         
     many :products, :foreign_key => :store_ids 
   end

   class Product
     include MongoMapper::Document         
     key :store_ids, Array, :typecast => 'ObjectId'
     many :stores, :in => :store_ids
   end

UPDATE

In trying Scott's suggestion, I've added the following to the Store model:

def self.all_including_nested
  stores = []
  Store.all.each do |store|
    stores << store.to_hash
  end
end

def to_hash
  keys = self.key_names
  hash = {}
  keys.each{|k| hash[k] = self[k]}
  hash[:products] = self.products
  hash[:services] = self.services
  hash
end

And in the controller:

def index
  @stores = Store.all_including_nested
  respond_with @stores
end

Which looks like it should work? Assuming the array of hashes would have #to_json called on it, and then the same would happen to each hash and each Product + Service. I'm reading through ActiveSupport::JSON's source, and so far that's what I've grokked from it.

But, not working yet... :(

Paley answered 24/2, 2011 at 17:19 Comment(0)
U
17

Have a look at the as_json() method. You put this in your models, define your json, and then simply call the render :json method and get what you want.

class Something
  def as_json(options={})
    {:account_name       => self.account_name,
     :expires_on         => self.expires_on.to_s,
     :collections        => self.collections,
     :type               => "Institution"}
  end
end

You'll notice self.collections which is a many relationship. That model also has as_json() defined:

class Collection
  def as_json(options={})
    {:name          => self.name,
     :title         => self.title,
     :isbn          => self.isbn,
     :publisher     => self.publisher,
     :monthly_views => self.monthly_views}
  end
end

This one contains self.monthly_views which represents another many relationship.

Then in your controller:

@somethings = Something.all
render :json => @somethings
Upheave answered 25/2, 2011 at 19:1 Comment(1)
in the knick of time, had just figured the same and was going to update the question with it ;) response much appreciated, thanksPaley
P
2

You might have to create your own method to generate a hash then turn the hash into JSON. I'm thinking something like this:

store = Store.first
keys = store.key_names
hash = {}
keys.each{|k| hash[k] = store[k]}
hash[:products] = store.products
hash.to_json
Presently answered 25/2, 2011 at 3:11 Comment(1)
Thanks, that makes sense - just found out the :include option only works on active_record's to_json. Got this working for a single instance, implementing a Store#to_json (just had to remove the hash = keys... assignment), but still have to figure out how to do it for collections. Weary of patching mongo_mapper itself :)Paley

© 2022 - 2024 — McMap. All rights reserved.