Mongoid 中除了 embeds_one 对象之外的 JSON 序列化?

发布于 2024-10-24 11:36:34 字数 463 浏览 2 评论 0原文

给定这个嵌入了一个位置对象的 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 技术交流群。

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

发布评论

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

评论(1

一抹淡然 2024-10-31 11:36:34

对我有用:


require 'rubygems'
require 'mongoid'

Mongoid::Config.settings = {
  "host" => "127.0.0.1",
  "database" => "testing"
}

class Report
  include Mongoid::Document
  embeds_one :location

  field :comment
end

class Location
  include Mongoid::Document
  field :address
  field :geocode, :type => Hash

  embedded_in :report
end

report = Report.new(:comment=>'comment', :location => {:address=>'elm street', :geocode => {:ll=>'-31'}})
puts report.to_json(:include=>:location, :except => :geocode)

产量:
{"_id":"4dab2655b4e4cf2fa6000001","comment":"comment","location":{"_id":"4dab2655b4e4cf2fa6000002","address":"elm street"}}

如果您想要位置,请始终忽略地理编码,那么你应该在Location类中重新定义serialized_hash:


  def serializable_hash(options = nil)
    options ||= {}
    options[:except] = :geocode
    super(options).tap do |attrs|
      serialize_relations(attrs, options) if options[:include]
    end
  end

Works for me:


require 'rubygems'
require 'mongoid'

Mongoid::Config.settings = {
  "host" => "127.0.0.1",
  "database" => "testing"
}

class Report
  include Mongoid::Document
  embeds_one :location

  field :comment
end

class Location
  include Mongoid::Document
  field :address
  field :geocode, :type => Hash

  embedded_in :report
end

report = Report.new(:comment=>'comment', :location => {:address=>'elm street', :geocode => {:ll=>'-31'}})
puts report.to_json(:include=>:location, :except => :geocode)

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:


  def serializable_hash(options = nil)
    options ||= {}
    options[:except] = :geocode
    super(options).tap do |attrs|
      serialize_relations(attrs, options) if options[:include]
    end
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文