title: Rails json object with model associations
body: <p>i'm new to Ruby and Rails. I have a problem constructing json response for my service.
  I have two models:</p>
  
  <pre><code>class Transaction &lt; ActiveRecord::Base
  has_and_belongs_to_many :users
  end
  
  class User &lt; ActiveRecord::Base
  has_and_belongs_to_many :transactions
  end
  </code></pre>
  
  <p>So users have collection of transactions and transaction has collections of users. What i'm trying to do is return all transactions where asking user is involved and other users, which involved in all of transactions.</p>
  
  <p>My json should look like this:</p>
  
  <pre><code>{"transactions":[{"name":"transaction1", "users":[{"userName":"user1"},{"userName":"user2"}]},{"name":"transaction2", "users":[{"userName":"user1"},{"userName":"user2"}]}]}
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>@user = User.find_by_facebookId(params[:id])
  
  #json = User.find_by_facebookId(params[:id], :include =&gt; :transactions).to_json( :include =&gt; :transactions )
  
  if @user != nil
     render json: {"transactions" =&gt; @user.transactions.map do |transaction|
           transaction
     end
     }
  else
    render json: {"transactions" =&gt; [{}]}
  end
  </code></pre>