2011-02-24 11 views
8
class Api::StoresController < ApplicationController 
    respond_to :json 

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

रिटर्न केवल स्टोर, करता हैJSON उत्तर में नेस्टेड वस्तुओं सहित, MongoMapper से अपने उत्पादों के बिना वस्तुओं

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

संघ परीक्षण किया जाता है, मैं, से कहते हैं सांत्वना ouput में नेस्टेड उत्पादों को देख सकते हैं,

Store.first.products 

उन्हें मोंगोमैपर के साथ उत्पादों को शामिल करने का सही तरीका क्या है?

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 

अद्यतन

स्कॉट के सुझाव की कोशिश कर रहा में, मैं स्टोर मॉडल के लिए निम्न जोड़ दिया है:

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 

और नियंत्रक में

यहाँ मेरी मॉडल हैं :

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

कौन सा दिखता है जैसे इसे काम करना चाहिए? हैश की सरणी मानते हुए #to_json पर कॉल किया जाएगा, और फिर प्रत्येक हैश और प्रत्येक उत्पाद + सेवा के साथ ऐसा ही होगा। मैं ActiveSupport :: जेएसओएन के स्रोत के माध्यम से पढ़ रहा हूं, और अब तक मैंने जो कुछ भी किया है, वह है।

लेकिन, अभी तक काम नहीं कर रहा ... :(

उत्तर

17

as_json() विधि पर एक नज़र डालें। आप अपने मॉडल में इस डाल दिया, अपने json को परिभाषित है, और फिर बस render :json विधि कॉल और आप क्या चाहते हैं मिलता है।

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 

आपको self.collections दिखाई देगा जो कई रिश्ते हैं। यही कारण है कि मॉडल भी as_json() परिभाषित किया गया है:

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 

यह एक self.monthly_views जो एक और अनेक संबंध का प्रतिनिधित्व करता है शामिल हैं।

फिर अपने नियंत्रक में:

@somethings = Something.all 
render :json => @somethings 
+0

समय के घुटनों में, बस यह वही लगा था और इसके साथ सवाल अपडेट करने जा रहा था;) प्रतिक्रिया बहुत सराहना की, धन्यवाद – oliverbarnes

2

आप अपनी खुद की विधि तो JSON में हैश एक हैश उत्पन्न करने के लिए बारी बनाने के लिए मैं कुछ इस तरह सोच रहा हूँ हो सकता है:।

store = Store.first 
keys = store.key_names 
hash = {} 
keys.each{|k| hash[k] = store[k]} 
hash[:products] = store.products 
hash.to_json 
+0

धन्यवाद, कि समझ में आता है - बस पता चला: केवल active_record के to_json पर काम करता है विकल्प शामिल हैं। यह एक ही उदाहरण के लिए काम कर रहा है, स्टोर # to_json को कार्यान्वित करना (केवल हैश = कुंजी ... असाइनमेंट को हटाना था), लेकिन अभी भी यह पता लगाना होगा कि संग्रह के लिए इसे कैसे किया जाए। Mongo_mapper खुद को पैच करने से थके हुए :) – oliverbarnes

संबंधित मुद्दे