Ruby on rails 数据库派生字段的跨类未定义方法

Ruby on rails 数据库派生字段的跨类未定义方法,ruby-on-rails,Ruby On Rails,我知道以前有人问过这个问题,但我相信我的问题是不同的;-) 我有2个RubyonRails类(带有关联的控制器、模型等)。这些是设备和消息 在Devices index.html视图中,我需要引用最新的消息记录 <table> <tr> <th>Device Name</th> <th>Checked</th> </tr> <% newestMessage = Message.find(

我知道以前有人问过这个问题,但我相信我的问题是不同的;-)

我有2个RubyonRails类(带有关联的控制器、模型等)。这些是设备和消息

在Devices index.html视图中,我需要引用最新的消息记录

<table>
  <tr>
    <th>Device Name</th>
<th>Checked</th>
  </tr>

 <% newestMessage = Message.find(:all, :order => "updated_at DESC", :limit => 1) %>

 <% @devices.each do |device| %>
    <tr>
      <td><%= device.name %></td>
  <td><%= newestMessage.updated_at %></td>
    </tr>
  <% end %>
</table>
然而,我已经尝试过了,它没有帮助。另外,我不认为这应该是必要的,因为“updated_at”是从数据库中派生出来的,是用scaffold等构建的

如果我只是打印“最新消息”,它似乎有一个合理的价值

我的问题是,我应该如何访问newestMessage中的字段,这些字段属于Device index.html.erb中的类Message


提前感谢。

试试
newestMessage=Message。最后一次
然后
newestMessage。在

更新,效果很好!谢谢(几分钟内我不会接受你的回答,但我会接受)。我真的很想了解为什么这样做有效,而我的原始代码却没有。
undefined method `updated_at' for #<Array:0x103f36c00>
<td><%= newestMessage.updated_at %></td>
  attr_accessor :updated_at