Multiple carrier mount_uploader on one model

I have a model called Recipe that has 2 images that use a carrier wave, so in this model I have this to set the carrier wave

mount_uploader :author_photo, AuthorUploader mount_uploader :photo, PhotoUploader 

I also added several versions to my images, such as large, small, medium, large

Problem. let's say i have 2 images

 Chocolate_Cake.jpg as the photo My_Photo.jpg as author_photo 

When I enter the console and upload my recipe and recipe.to_json, I get both of my images back from the carrier, but they both show a photograph for the recipe, not a photograph of the author.

  "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 answer is not showing my loaders properly.

If I were to enter this in the console,

  recipe.photo recipe.author_photo 

They come up with different image urls

+4
source share
1 answer

After some research and help from a friend, I found that I could override the as_json method for the recipe model to fix the answer 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 works.

+5
source

Source: https://habr.com/ru/post/1412492/


All Articles