如何显示链接模型的项目

时间:2017-06-08 09:12:14

标签: ruby-on-rails has-many

我有2个模型(“Offense”和“OffenceType”),其中has_many关系由连接表“lk_offence_types”管理。 在“进攻”的index.html.erb中,我无法显示OffenceType项。 (我尝试过代理,但似乎无法使用has_many ..) 进攻模式: class Offense< ApplicationRecord     has_many:lk_offence_types,inverse_of :: crime,dependent :: destroy     has_many:offence_types,通过:: lk_offence_types     #delegate:offence_type_description,to :: offence_type 结束 OffenceType型号: class OffenceType< ApplicationRecord     has_many:lk_offence_types     has_many:犯罪,通过:: lk_offence_types 结束 LkOffenceType型号: class LkOffenceType< ApplicationRecord   belongs_to:offence_type,inverse_of :: lk_offence_types   belongs_to:attack,inverse_of :: lk_offence_types 结束 进攻的index.html.erb: <%@ offences.each do | attack | %GT; < TR> < td><%= offence.offence_description%>< / td> < td><%= offence.offence_types.offence_type_description%>< / td> <%end%> <%= offence.offence_types.offence_type_description%>给了我一个错误。 我也尝试过: <%= offence.offence_type_description%>和其他siyntaxes ... 我错过了什么? (我使用的是Ruby 2.3和Rails 5.0.2) 谢谢

1 个答案:

答案 0 :(得分:1)

您还需要遍历offence types。您可能需要修复样式,这只是为了让您了解缺少的内容。

<% offence.offence_types.each do |ot| %>
 <td><%= ot.offence_type_description %></td>
<% end %>