I tried a couple of other similar messages, but I still get the error message.
In the Messages model, I have a category_id field. I have the following models:
#Posts model
belongs_to :categories
#Category model
has_many :posts
The message index controller has:
@categories = @posts.Category.find(:all, :order => 'categoryname')
In the view, I have:
<% @posts.each do |post| %>
<tr>
<td><%= post.category_id %></td>
<td><%= @categories.categoryname %></td>
<td><%= link_to 'View', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
</tr>
<% end %>
In the second column, I'm trying to show the category name ("categoryname") from the Category table, not the category_id from the posts table. I get an error message:
undefined `Category 'method for #ActiveRecord :: Relation: 0x3e1a9b0>
I also tried:
<td><%= post.categories.categoryname %></td>
But get the same error.
As well as:
<td><%= post.category.categoryname %></td>
Any suggestions would be greatly appreciated.
source
share