I want to be able to dynamically undefine mongoid fields. I tried the undef method in the context of the class object. Unfortunately it does not work, as shown below:
class MongoTest
include Mongoid::Document
field :abc, type: Integer
field :def, type: String
end
m = MongoTest.new
m.fields.keys
=> ["_id", "abc", "def"]
MongoTest.class_eval { undef :abc, :abc= }
m.fields.keys
=> ["_id", "abc", "def"]
But undef does actually undefine the method from being invocable:
m.abc
NoMethodError: undefined method `abc' for #<MongoTest _id: 554013e86d61632f57000000, abc: nil, def: nil>
I'm a little confused as to why this method still shows up. What am I doing wrong?