How to customize to_json response in Rails 3

14,050

Solution 1

It is not the rails 3 built-in way, but I found a great gem that is actively maintained on Rails 3: acts_as_api

Solution 2

You can override as_json in your model. Something like:

class Post < ActiveRecord::Base
  def as_json(options = {})
    {
      attribute: self.attribute, # and so on for all you want to include
      images:    self.images,    # then do the same `as_json` method for Image
      foo:       self.really_cool_method
    }
  end
end

And Rails takes care of the rest when using respond_with. not entirely sure what options gets set to, but probably the options you give to respond_with (:include, :only and so on)

Solution 3

Probably too late, but I found a more DRY solution digging through the rails docs. This works in my brief tests, but may need some tweaking:

# This method overrides the default by forcing an :only option to be limited to entries in our
# PUBLIC_FIELDS list
def serializable_hash(options = nil)
  options ||= {}
  options[:only] ||= []
  options[:only] += PUBLIC_FIELDS
  options[:only].uniq!
  super(options)
end

This basically allows you to have a list of fields that are allowed for your public API, and you cannot accidentally expose the whole object. You can still expose specific fields manually, but by default your object is secure for .to_json, .to_xml, etc.

Share:
14,050
coneybeare
Author by

coneybeare

http://matt.coneybeare.me @coneybeare I have had a ton iOS apps under my name and my business name. I have multiple client apps, and one from an established startup. Many of my apps have been featured by Apple. I developed two iOS apps that made it into the top 100. One of the apps held the number two spot for three weeks. Another floats in/out of the paid News top 10. My apps have over 10 million downloads. I do a ton of web work too using rails for all my sites. I do much of my own design for all the sites I have created.

Updated on June 07, 2022

Comments

  • coneybeare
    coneybeare almost 2 years

    I am using respond_with and everything is hooked up right to get data correctly. I want to customize the returned json, xml and foobar formats in a DRY way, but I cannot figure out how to do so using the limited :only and :include. These are great when the data is simple, but with complex finds, they fall short of what I want.

    Lets say I have a post which has_many images

    def show
      @post = Post.find params[:id]
      respond_with(@post)
    end
    

    I want to include the images with the response so I could do this:

    def show
      @post = Post.find params[:id]
      respond_with(@post, :include => :images)
    end
    

    but I dont really want to send the entire image object along, just the url. In addition to this, I really want to be able to do something like this as well (pseudocode):

    def show
      @post = Post.find params[:id]
      respond_with(@post, :include => { :foo => @posts.each.really_cool_method } )
    end
    
    def index
      @post = Post.find params[:id]
      respond_with(@post, :include => { :foo => @post.really_cool_method } )
    end
    

    … but all in a DRY way. In older rails projects, I have used XML builders to customize the output, but replicating it across json, xml, html whatever doesnt seem right. I have to imagine that the rails gurus put something in Rails 3 that I am not realizing for this type of behavior. Ideas?

  • Nerdmaster
    Nerdmaster about 13 years
    Actually, this should probably just default when :only isn't specified, otherwise it changes the intent of using :only. Oh, well, still my point is the same.
  • scragz
    scragz about 13 years
    He wants to change as_json, as_xml, as_foobar all in one place.
  • Docunext
    Docunext over 11 years
    Overriding serializable_hash worked better for me to add in some methods as attributes in Rails 3.2.8, thanks!
  • Meekohi
    Meekohi over 8 years
    include does not use as_json, this doesn't help.