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

| 给定这个杂物\“ Report \” Document对象,它们的embeds_one位置对象是:
{
  comment: \"\",
  location: {
    address: \"391 Little Bourke St, Melbourne VIC 3000, Australia\",
    geocode: {
      ll: \"-37.813787,144.961227\",
    }
  },
  _id: \"4d84af7f52f3d40539000021\",
}
除了位置哈希的地址解析部分,我该如何处理? 例如,通过在Report模型中执行以下操作可以很容易地排除注释字段:
def to_json(options={})
  options[:except] ||= :comment
  super(options)
end
除位置代码的一部分“地理代码”外,代码的外观如何?     
已邀请:
为我工作:
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 \”,\“注释\”:\“注释\”,\“位置\”:{\“ _ id \”:\“ 4dab2655b4e4cf2fa6000002 \”,\“地址\”: \“榆树街\”}} 并且如果您希望Location始终省略地理编码,则应在Location类中重新定义serializable_hash:
  def serializable_hash(options = nil)
    options ||= {}
    options[:except] = :geocode
    super(options).tap do |attrs|
      serialize_relations(attrs, options) if options[:include]
    end
  end
    

要回复问题请先登录注册