title: Ruby on Rails Advanced JSON Serialization
body: <p>I'm looking to render an index of all articles along with a full article via JSON in my rails app, but I'm having a little trouble figuring out how to do it.</p>
  
  <p>Here is my controller now:</p>
  
  <pre><code>if params[:id]
      @article = Article.find(params[:id])
  else
      @article = Article.published.not_draft.by_recent.first
  end
  
  respond_to do |format|
  
  format.js { render :json =&gt; @article.to_json(
  :except =&gt; [ :created_at, :updated_at, :draft, :id, :publish ], 
  :include =&gt; {
      :comments =&gt; {
          :only =&gt; [:body]
      }
  }),
  :callback =&gt; params[:callback]}
  end
  </code></pre>
  
  <p>What I'd like to do in the response is add an index of all articles, like so:</p>
  
  <pre><code>@index = Article.find(:all, :select =&gt; 'id, title')
  </code></pre>
  
  <p>The only way I've been able to do it, is put both the index and article into a hash or array and then put that to JSON.</p>
  
  <pre><code>@response = { :item =&gt; @article, :index =&gt; @index }
  </code></pre>
  
  <p>Full code with both:</p>
  
  <pre><code>@index = Article.find(:all, :select =&gt; 'id, title')
  
  if params[:id]
      @article = Article.find(params[:id])
  else
      @article = Article.published.not_draft.by_recent.first
  end
  
  @response = { :item =&gt; @article, :index =&gt; @index }
  
  respond_to do |format|
  
  format.js { render :json =&gt; @response.to_json(), :callback =&gt; params[:callback]}
  
  end
  </code></pre>
  
  <p>This would be fine, except now I cannot specify <code>:include</code> or <code>:except</code> and get it to render properly.</p>