在 JSON 响应中包含来自 MongoMapper 对象的嵌套对象
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下
as_json()
方法。您将其放入模型中,定义 json,然后只需调用 render :json 方法即可获得您想要的内容。您会注意到
self.collections
这是一个多关系。该模型还定义了as_json()
:这个模型包含
self.monthly_views
,它代表另一种多关系。然后在你的控制器中:
Have a look at the
as_json()
method. You put this in your models, define your json, and then simply call therender :json
method and get what you want.You'll notice
self.collections
which is a many relationship. That model also hasas_json()
defined:This one contains
self.monthly_views
which represents another many relationship.Then in your controller:
您可能必须创建自己的方法来生成哈希,然后将哈希转换为 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: