4

I have a model called Recipe which has 2 images that use carrierwave, so in this model i have this to setup carrierwave

mount_uploader :author_photo, AuthorUploader
mount_uploader :photo, PhotoUploader

I have also added multiple version to my images such as thumb, small, medium, large

The problem is. say i have 2 images

Chocolate_Cake.jpg as the photo
My_Photo.jpg as author_photo

When i go into console and load up my recipe and to recipe.to_json, I get both my images back from carrierwave but they are both showing the photo for the recipe, not the author photo.

     "recipe": [
    {
        "author_photo": {
            "url": "/uploads/recipe/photo/8/Chocolate_Cake.jpg",
            "thumb": {
                "url": "/uploads/recipe/photo/8/thumb_Chocolate_Cake.jpg"
            },
            "small": {
                "url": "/uploads/recipe/photo/8/small_Chocolate_Cake.jpg"
            },
            "medium": {
                "url": "/uploads/recipe/photo/8/medium_Chocolate_Cake.jpg"
            },
            "large": {
                "url": "/uploads/recipe/photo/8/large_Chocolate_Cake.jpg"
            }
        },
        "id": 8,
        "photo": {
            "url": "/uploads/recipe/photo/8/Chocolate_Cake.jpg",
            "thumb": {
                "url": "/uploads/recipe/photo/8/thumb_Chocolate_Cake.jpg"
            },
            "small": {
                "url": "/uploads/recipe/photo/8/small_Chocolate_Cake.jpg"
            },
            "medium": {
                "url": "/uploads/recipe/photo/8/medium_Chocolate_Cake.jpg"
            },
            "large": {
                "url": "/uploads/recipe/photo/8/large_Chocolate_Cake.jpg"
            }
        },

So for some reason my json response isnt showing my uploaders properly.

If i was to type this in console,

 recipe.photo
 recipe.author_photo

They come up with different image urls

Robert
  • 596
  • 1
  • 7
  • 20

1 Answers1

5

After some research and a help from a friend, i found that i could override the as_json method for the recipe model to fix the response i was getting.

   def as_json(options = {})
     super.merge('photo' => photo.as_json[:photo], 'author_photo' => author_photo.as_json[:author_photo])
   end

This solution worked.

Robert
  • 596
  • 1
  • 7
  • 20
  • This worked for me as well but have you found out the cause of this. Feels like a bug in CarrierWave or usage. – Gerry Shaw May 17 '12 at 01:06
  • Hey Gerry, not sure but to me it seems like it uses the last uploader you set, it does sound like a possible bug/usage. – Robert May 22 '12 at 01:46