我有2个模型("攻击"one_answers" forceCeType"(,其中有一个has_many关系由JOIN表" lk_offence_types"管理。
在"犯罪"的index.html.erb中,我无法显示一个标准项目。(我已经尝试了委派,但似乎不适合has_many ..(
进攻模型:
class Offence < ApplicationRecord
has_many :lk_offence_types, inverse_of: :offence, dependent: :destroy
has_many :offence_types, through: :lk_offence_types
#delegate :offence_type_description, to: :offence_type
end
关注模型:
class OffenceType < ApplicationRecord
has_many :lk_offence_types
has_many :offences, through: :lk_offence_types
end
lkoffenceType模型:
class LkOffenceType < ApplicationRecord
belongs_to :offence_type, inverse_of: :lk_offence_types
belongs_to :offence, inverse_of: :lk_offence_types
end
进攻索引.html.erb:
<% @offences.each do |offence| %>
<tr>
<td><%= offence.offence_description %></td>
<td><%= offence.offence_types.offence_type_description %></td>
<% end %>
&lt;%= rence.offence_types.offence_type_description%>给我一个错误。我也尝试了:&lt;%= rence.offence_type_description%>和其他siyntaxes ...
我缺少什么?
(我使用Ruby 2.3和Rails 5.0.2(
谢谢
您也需要在offence types
上循环。您可能需要修复样式,这只是为了让您了解缺少的内容。
<% offence.offence_types.each do |ot| %>
<td><%= ot.offence_type_description %></td>
<% end %>