This helped for display on an individual ‘items’ show page
Accessing an attribute of a linked model in Rails
However I’m having trouble doing the same for an ‘all items table’
...
<% @items.each do |item| %>
...
<td><%= item.room.name %></td>
...
Clearly where one room has many items.
only this works:
<td><%= item.room_id %></td>
I can’t seem to use it there, gives:
undefined method `name' for nil:NilClass
Have a look at Rails’ Delegate module:
This will add the instance method
room_nametoItem, which will fail more gracefully (returningnilif there is no room, instead of theNilClasserror).