Search code examples
ruby-on-railsjsonrabl

Mistaken duplicate JSON nested object - Rails


I have a Rails API that returns JSON using the rabl gem. Here's the /show template which is extended to the /index template as well.

object @deal
attributes :headline, :text, :image, :id, :created_at

child :vendor do
    attributes :image_url, :id
end

Both Deal and Vendor have images, uploaded through CarrierWave/Fog to S3. The problem I'm seeing here is that my returned JSON includes and extra image object for deal. Like so:

"deal":{"image":{"image":{"url":"httpxxxxxxxxxxxx"}}}

In contrast, the JSON for vendor returns appropriately:

"vendor":{"image_url":"httpxxxxxxxxxxx"}

My uploaders are like below:

class DealUploader < CarrierWave::Uploader::Base
   def store_dir
     "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
   end
end

# vendor image uploader
class ImageUploader < CarrierWave::Uploader::Base
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

Why am I getting this redundant image object?


Solution

  • Try

    object @deal
    attributes :headline, :text, :image_url, :id, :created_at
    
    child :vendor do
        attributes :image_url, :id
    end