Rails 7: How to rename as_json keys

76 Views Asked by At

I have a rails 7 api that returns a nested json file, but I want to change one of the names. I have tried many things but none of them works all the methods I find online are outdated and don't work, here is one of my failed attempts:

  def as_json(options = {})
    super(options.merge(include:
      {
        property: {
          # except: :volume,
          # rename: { volume: :size }
          hash['size'] = hash.delete('volume')
        }
      }, 
    ))
  end
0

There are 0 best solutions below