3

I have a user model which mount a carrierwave uploader for a field called profile_img. code folows:

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

the output of profile_img field is 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 customize serialize hash by simply calling super,

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

the profile_image field key get 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 issue rooted from carrierwave serialize method, but can not find a solution on it.

Any clue ?

Community
  • 1
  • 1
fuyi
  • 2,573
  • 4
  • 23
  • 46

1 Answers1

0

solution for:

  1. jBuilder

    json.profile_img @user.profile_img.serializable_hash
    
  2. ActiveModelSerializer

    # attributes
    def profile_img
      object.profile_img.serializable_hash
    end
    
chinloong
  • 3,507
  • 1
  • 21
  • 20