Rails从另一个表获取数据

时间:2016-11-19 13:02:44

标签: ruby-on-rails ruby

应用程序/模型/ road.rb

C

应用程序/模型/ segment.rb

class Road < ActiveRecord::Base
  has_many :segments
end

应用程序/控制器/ welcome_controller.rb

class Segment < ActiveRecord::Base
  belongs_to :road
end

应用程序/视图/欢迎/ index.html.erb

@segments = Segment.joins(:road).select("segments.id, segments.name,
segments.road_id, roads.name AS roadname, ST_AsGeoJson(segments.geom) AS
segment_geom")

重新加载页面时我得到了这个:

<script>
  <% @segments.each do |segment| %>
    var segment<%= segment.id %> = new L.geoJson((<%= segment.segment_geom.to_s.html_safe %>), {style: statesegmentStyle})
    .bindPopup('id: <%= segment.id %><br>road_name: <%= roadname %>').addTo(segments);
  <% end %>
<script>

0 个答案:

没有答案