0

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?

Donato
  • 2,727
  • 6
  • 29
  • 59
  • `undef` will remove the methods that Mongoid's `field` adds but it won't do anything to the fields themselves. Why are you trying to undo `field` calls? – mu is too short Apr 29 '15 at 00:05
  • @muistooshort I am building a dynamic CRM, where you can add and remove fields to objects through a UI. When someone selects to delete field, I just don't want that field in the system anymore. – Donato Apr 29 '15 at 00:48
  • 1
    Are you sure Mongoid is the right tool for this job? Removing the field requires editing the `MongoTest` class not just the database. – mu is too short Apr 29 '15 at 01:08
  • @muistooshort don't you think mongodb is a better choice than mysql or postgresql for dynamic fields? – Donato Apr 29 '15 at 16:39
  • 1
    I don't think it is a question of MongoDB versus SQL, PostgreSQL or MySQL might even be better since you can ask a table what its schema is but each document in a MongoDB collection can potentially be different. The main issue is whether or not a standard ORM/ODM such as Mongoid or ActiveRecord is the right tool. – mu is too short Apr 29 '15 at 16:59

1 Answers1

1

Instead of defining static fields on the model and attempting to remove them at runtime, you should use dynamicfields in the first place.

In order to use dynamic fields you must add the following line to your model:

include Mongoid::Attributes::Dynamic

And according to this question you will need to set allow_dynamic_fields: true in mongoid.yml

Community
  • 1
  • 1
Mike S
  • 11,329
  • 6
  • 41
  • 76
  • Thanks for the idea, the only thing is when I want to get all the field keys e.g. m.fields.keys, dynamic fields are not present. – Donato Apr 29 '15 at 18:59