title: Rails 3: How to display many-to-many join in view?
body: <p>I'm trying to display all the 'legs' for each of my 'trips' in my view.</p>
  
  <p>Ideally they would look like this:</p>
  
  <pre><code>-Trip1.name
    - Leg1.name
    - Leg2.name
    - Leg3.name
  -Trip2.name
    - Leg4.name
    - Leg5.name
    - Leg6.name 
  </code></pre>
  
  <p>The models are many-to-many. I have three tables Legs, Trips and Legs_trips. The models look like this:</p>
  
  <p>Trip.rb</p>
  
  <pre><code>class Trip &lt; ActiveRecord::Base
   has_and_belongs_to_many :legs
   accepts_nested_attributes_for :legs, :reject_if =&gt; :all_blank
  end
  </code></pre>
  
  <p>Leg.rb</p>
  
  <pre><code>class Leg &lt; ActiveRecord::Base
   has_and_belongs_to_many :trips
   validates_uniqueness_of :name
   accepts_nested_attributes_for :trips
  end
  </code></pre>
  
  <p>There is nothing but this in the controller at the moment:</p>
  
  <pre><code>def index
  @trips = Trip.all
  @legs = Leg.all
  
   respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @trips }
   end
  end
  </code></pre>
  
  <p>Any suggestions for how to structure my view would be great! I've tinkered around for ages but can't seem to get anywhere near or find anything online.  </p>
  
  <p>Thanks in advance!</p>
  
  <p>James</p>