1

I'm trying to use IPAddress as a custom field for a Mongoid Document by monkey patching the serialization methods to the IPAddress module and I just can't seem to get it...

class Computer
  include Mongoid::Document

  field :ip_address, type: IPAddress
end

module IPAddress
  def mongoize
    to_string
  end

  def self.mongoize(object)
    object.to_string
  end

  def self.demongoize(string)
    IPAddress.new string
  end

  def self.evolve(object)
    object.to_string
  end
end

Here's what I got right this second... but I've tried lots of other ways and just can't find one that works. Any help would be much appreciated!

  • What errors are you getting? What versions are you using? Also in the Computer class aren't you missing include Mongoid::Document? – Novae Feb 12 '13 at 23:02
  • You are missing a .new inside demongoize (`IPAddress.new(string)`) – Speed Mar 27 '13 at 16:10

0 Answers0