title: Rails getting data from another table
body: <p>app/models/road.rb</p>
  
  <pre><code>class Road &lt; ActiveRecord::Base
    has_many :segments
  end
  </code></pre>
  
  <p>app/models/segment.rb</p>
  
  <pre><code>class Segment &lt; ActiveRecord::Base
    belongs_to :road
  end
  </code></pre>
  
  <p>app/controllers/welcome_controller.rb</p>
  
  <pre><code>@segments = Segment.joins(:road).select("segments.id, segments.name,
  segments.road_id, roads.name AS roadname, ST_AsGeoJson(segments.geom) AS
  segment_geom")
  </code></pre>
  
  <p>app/views/welcome/index.html.erb</p>
  
  <pre><code>&lt;script&gt;
    &lt;% @segments.each do |segment| %&gt;
      var segment&lt;%= segment.id %&gt; = new L.geoJson((&lt;%= segment.segment_geom.to_s.html_safe %&gt;), {style: statesegmentStyle})
      .bindPopup('id: &lt;%= segment.id %&gt;&lt;br&gt;road_name: &lt;%= roadname %&gt;').addTo(segments);
    &lt;% end %&gt;
  &lt;script&gt;
  </code></pre>
  
  <p>when reload the page I get this:</p>
  
  <pre><code>undefined local variable or method `roadname' for #&lt;#&lt;Class:0x00000003a14ef8&gt;:0x007f7ea4628d90&gt;
  </code></pre>