Ruby metaprogramming with MongoMapper -


I'm dynamically trying to add keys to monomomer documents.

  def build (attrs = {}) Doctor = self.new apply_scope (doctor) doc.set_up_keys! Doc.attributes = attrs Doctor End Def set_up_keys! Do form form.fields.each as long as possible. F | | Next if self.keys.include? (F.underscored_name) self.class.send (: key, f.underscored_name, f.keys ['default']. Type: Required => f.required, default => F.default) End End  

The code in question is available and.

form is a related model I want to make the key on the current model (self) based on the form # field .

The problem is that if I make two models, then both of them have the same key from both models

self.class.send (: key ...) < / Code> adds keys to the model.

Why are they being added to both models?

/ P>

Is this because the method is being said in the context of class?

How can I only influence personal examples?

Defines a model by mangomapper, its orbit class shares the keys of all the example models If you want to create a model on the fly, you will probably need a key for it, and add the key for it:

  def build (attrs = {}) c = Class.new ( Self.class Doctor = c.new apply_scope (doctor) doc.set_up_keys! Doc.attributes = attrs Doctor End  

Comments