Rails - 获取相关记录的数据

时间:2016-09-06 07:15:48

标签: ruby-on-rails

RoR新手,我试图让相关记录的数据显示在索引中。

我让这个工作:

<td><%= annotation.documenttype_id %></td>

但是想要文档类型的name字段的值。 我正在使用Rails 5.到目前为止,体验Rails,它必须如此简单......

2 个答案:

答案 0 :(得分:0)

<td><%= annotation.document_type.field_name %></td>

答案 1 :(得分:0)

从您的代码我可以得到关联:

  

注释belongs_to document_type

所以你可以从这样的注释中访问document_type:

annotation.document_type.name