title: How to customize to_json response in Rails 3
body: <p>I am using <code>respond_with</code> and everything is hooked up right to get data correctly.  I want to customize the returned <code>json</code>, <code>xml</code> and <code>foobar</code> formats in a DRY way, but I cannot figure out how to do so using the limited <code>:only</code> and <code>:include</code>. These are great when the data is simple, but with complex finds, they fall short of what I want.</p>
  
  <p>Lets say I have a post which <code>has_many</code> images</p>
  
  <pre><code>def show
    @post = Post.find params[:id]
    respond_with(@post)
  end
  </code></pre>
  
  <p>I want to include the images with the response so I could do this:</p>
  
  <pre><code>def show
    @post = Post.find params[:id]
    respond_with(@post, :include =&gt; :images)
  end
  </code></pre>
  
  <p>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):</p>
  
  <pre><code>def show
    @post = Post.find params[:id]
    respond_with(@post, :include =&gt; { :foo =&gt; @posts.each.really_cool_method } )
  end
  
  def index
    @post = Post.find params[:id]
    respond_with(@post, :include =&gt; { :foo =&gt; @post.really_cool_method } )
  end
  </code></pre>
  
  <p>… 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?</p>