List dynamic attributes in a Mongoid Model

后端 未结 6 1362
醉酒成梦
醉酒成梦 2021-02-06 15:49

I have gone over the documentation, and I can\'t find a specific way to go about this. I have already added some dynamic attributes to a model, and I would like to be able to it

6条回答
  •  梦如初夏
    2021-02-06 16:29

    So, what I ended up doing is this. I'm not sure if it's the best way to go about it, but it seems to give me the results I'm looking for.

    class Order
      def dynamic_attributes
        self.attributes.delete_if { |attribute| 
          self.fields.keys.member? attribute 
        }
      end
    end
    

    Attributes appears to be a list of the actual attributes on the object, while fields appears to be a hash of the fields that were predefined. Couldn't exactly find that in the documentation, but I'm going with it for now unless someone else knows of a better way!

提交回复
热议问题