ruby - undefine methods in mongoid -


i want able dynamically undefine mongoid fields. tried undef method in context of class object. unfortunately not work, 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 undefine method being invocable:

m.abc nomethoderror: undefined method `abc' #<mongotest _id: 554013e86d61632f57000000, abc: nil, def: nil> 

i'm little confused why method still shows up. doing wrong?

instead of defining static fields on model , attempting remove them @ runtime, should use dynamicfields in first place.

in order use dynamic fields must add following line model:

include mongoid::attributes::dynamic 

and according this question need set allow_dynamic_fields: true in mongoid.yml


Comments