开发者

Undefined Method, across classes, for a database derived field

I'm aware that variants of this question have been asked before, but I do believe my question is different ;-)

I have 2 Ruby on Rails classes (with assoc. controllers, models, etc). These are Devices and Messages.

In the Devices index.html view, I need to make reference to the newest Message record.

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

 <% newestMessage = Message.find(:all, :order => "updated_at DESC", :limit => 1) %&g开发者_如何学Got;

 <% @devices.each do |device| %>
    <tr>
      <td><%= device.name %></td>
  <td><%= newestMessage.updated_at %></td>
    </tr>
  <% end %>
</table>

(I believe I should move the logic to a helper, but for the time being I'm keeping it as simple as possible while I get it working)

When I access this blahblah.com/devices, I get...

undefined method `updated_at' for #<Array:0x103f36c00>

Due to the line,

<td><%= newestMessage.updated_at %></td>

Now, I've seen questions like this that suggest to add the equivalent of the following to messages_controller.rb,

  attr_accessor :updated_at

However, I've tried this and it doesn't help. Also, I don't think this should be necessary anyway as 'updated_at' is derived from the database, built with scaffold, etc.

If I just print 'newestMessage' it seems to have a sensible value.

My question is, how should I access fields within newestMessage, which are of class Message from the Device index.html.erb?

Thanks in advance.


Try newestMessage = Message.last then newestMessage.updated_at

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜