Bidirectional operator key in serializable_hash

I have a custom model that mounts a carrier loader for a field named profile_img. code:

class User < ActiveRecord::Base
    mount_uploader :profile_img, ProfileUploader
end

      

the output of the profile_img field looks like this:

"profile_img": {
            "url": "https://halo-img-dev.s3.amazonaws.com/uploads/user/profile_img/1000/cola_iOS2_512_dribbble2.png",
            "thumb": {
                "url": "https://halo-img-dev.s3.amazonaws.com/uploads/user/profile_img/1000/thumb_cola_iOS2_512_dribbble2.png"
            },
            "medium": {
                "url": "https://halo-img-dev.s3.amazonaws.com/uploads/user/profile_img/1000/medium_cola_iOS2_512_dribbble2.png"
            }
        }

      

When I try to set up hash serialization just by calling super,

def serializable_hash(options = {})
    super(options)
end

      

the profile_image field field will be duplicated

"profile_img": {
        "profile_img": {  #### duplicate here
            "url": "https://halo-img-dev.s3.amazonaws.com/uploads/user/profile_img/1000/cola_iOS2_512_dribbble2.png",
            "thumb": {
                "url": "https://halo-img-dev.s3.amazonaws.com/uploads/user/profile_img/1000/thumb_cola_iOS2_512_dribbble2.png"
            },
            "medium": {
                "url": "https://halo-img-dev.s3.amazonaws.com/uploads/user/profile_img/1000/medium_cola_iOS2_512_dribbble2.png"
            }
        }
    },

      

I suspect this question comes from the serialwave-media method, but cannot find a solution on it.

Any hint?

+3


source to share


1 answer


for:



  • JBuilder

    json.profile_img @user.profile_img.serializable_hash
    
          

  • ActiveModelSerializer

    # attributes
    def profile_img
      object.profile_img.serializable_hash
    end
    
          

0


source







All Articles