Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4015154
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:35:04+00:00 2026-05-20T09:35:04+00:00

class Api::StoresController < ApplicationController respond_to :json def index @stores = Store.all(:include => :products) respond_with

  • 0
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… 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:35:05+00:00Added an answer on May 20, 2026 at 9:35 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to update my SQL lite database with the native update-method of
My question is about memory use and objects in actionscript 2. If I have
I was reading JavaScript: The Good Parts and the author mentions that JavaScript is
I'm getting an error here that says I haven't defined a method, but it
I'm trying to write test harness for part of my Android mapping application. I
Let say I have the following desire, to simplify the IConvertible's to allow me
I want the messagebox to only show if the number is equal to 0.
After having read Ian Boyd 's constructor series questions ( 1 , 2 ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.