Field aliases with Mongoid and Rails

前端 未结 3 710
萌比男神i
萌比男神i 2021-01-02 13:56

It\'s common practice in MongoDB to use short key names to save space. For example, one might want to use \"fn\" instead of \"first_name\"

However in your app, you\

3条回答
  •  情书的邮戳
    2021-01-02 14:46

    Something like this should work in your class file. (It hasn't been tested in all cases, just passing a hash into create or update.)

      field :fn, :type => String
    
      alias :first_name :fn
      alias :filtered_process :process
    
      def process(attrs = nil)
        attrs[:fn] = attrs[:first_name] unless attrs.nil?
        filtered_process(attrs)
      end
    

提交回复
热议问题