Mongoid 中除了 embeds_one 对象之外的 JSON 序列化?
给定这个嵌入了一个位置对象的 mongoid“报告”文档对象:
{
comment: "",
location: {
address: "391 Little Bourke St, Melbourne VIC 3000, Australia",
geocode: {
ll: "-37.813787,144.961227",
}
},
_id: "4d84af7f52f3d40539000021",
}
如何排除位置哈希的地理编码部分?
例如,通过在报告模型中执行以下操作,可以轻松排除注释字段:
def to_json(options={})
options[:except] ||= :comment
super(options)
end
除了“地理编码”(位置的一部分)之外,代码是什么样的?
Given this mongoid "Report" Document object that embeds_one location object:
{
comment: "",
location: {
address: "391 Little Bourke St, Melbourne VIC 3000, Australia",
geocode: {
ll: "-37.813787,144.961227",
}
},
_id: "4d84af7f52f3d40539000021",
}
How do I except the geocode part of the location hash?
As an example, it's easy to except the comment field by doing this in the Report model:
def to_json(options={})
options[:except] ||= :comment
super(options)
end
What's the code look like to except "geocode", which is part of location?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我有用:
产量:
{"_id":"4dab2655b4e4cf2fa6000001","comment":"comment","location":{"_id":"4dab2655b4e4cf2fa6000002","address":"elm street"}}
如果您想要位置,请始终忽略地理编码,那么你应该在Location类中重新定义serialized_hash:
Works for me:
yields:
{"_id":"4dab2655b4e4cf2fa6000001","comment":"comment","location":{"_id":"4dab2655b4e4cf2fa6000002","address":"elm street"}}
and if you want the Location always omit the geocode, then you should re-define the serializable_hash inside Location class: