在 JSON 响应中包含来自 MongoMapper 对象的嵌套对象

发布于 2024-10-19 05:25:50 字数 1382 浏览 2 评论 0原文

class Api::StoresController < ApplicationController  
  respond_to :json

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

仅返回没有产品的商店,正如

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

关联经过测试一样,我可以在控制台输出中看到嵌套产品,比如说,

Store.first.products

让他们的产品包含在 MongoMapper 中的正确方法是什么?

以下是我的模型:

   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

更新

在尝试 Scott 的建议时,我已将以下内容添加到 Store 模型中:

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::JSON 的源代码,到目前为止,这就是我从中得到的内容。

但是,还没有工作......:(

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... :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有木有妳兜一样 2024-10-26 05:25:50

看一下 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

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
心不设防 2024-10-26 05:25:50

您可能必须创建自己的方法来生成哈希,然后将哈希转换为 JSON。我在想这样的事情:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文