我有两个模型: 1)上传 2) 日期范围
有一个中间连接表,因为这些模型是通过多对多关系关联的,因此,每个模型都是彼此的 habtm。
在我的上传 View (index.html.erb)中,我试图显示特定上传的所有日期范围,如下所示:
<tr>
<th>File name</th>
<th>Upload Date, Time, Filename</th>
<th>Type</th>
<th>Dates in Upload</th>
<th>Total Rows</th>
<th>Rows Entered in DB</th>
<th>Percentage Completed</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @uploads.each do |u| %>
<tr>
<td> <%= u.sourcedata_file_name%></td>
<% path_arr = u.f_path.split("\/")%>
<td><%= path_arr[-3..-1]%></td>
<td> <%= u.sourcedata_content_type%></td>
=>> <td> <%= u.date_ranges.inspect%>
<td> <%= u.total_rows%></td>
<td> <%= u.rows_completed%></td>
像这样。
这在浏览器上显示如下:
在我的“上传日期”列中,我只想显示一些带有日期的字符串,如下所示: “2013-12-25、2013-12-26”如何只从 ActiveRecord::Associations::CollectionProxy 对象中提取这些信息,如图所示?
谢谢
最佳答案
使用 u.date_ranges.pluck(:date_range)
获取日期范围。
然后你可以用
来美化它u.date_ranges.pluck(:date_range).each {|range| puts range}
如果您希望它们在一列中。
我看到你想要并排放置它们,所以看起来只有两个,因为它是一个“范围”,所以:
<%= u.date_ranges.pluck(:date_range).first %>, <%= u.date_ranges.pluck(:date_range).last %>
关于ruby-on-rails - 如何从我 View 中的 Rails habtm 集合对象中提取相关属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20819152/