Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7992319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:31:42+00:00 2026-06-04T13:31:42+00:00

I have tried a couple other similar posts but am still getting an error.

  • 0

I have tried a couple other similar posts but am still getting an error.

In the Posts model I have a category_id field. I have the following models:

#Posts model
belongs_to :categories  

#Category model
has_many :posts

In the Posts index controller I have:

@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 2nd column I am trying to show the category name (“categoryname”) from the Category table instead of the category_id from the posts table. I am getting an error:

undefined method `Category’ for #ActiveRecord::Relation:0x3e1a9b0>

I have 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:31:43+00:00Added an answer on June 4, 2026 at 1:31 pm

    In your model

    belongs_to :category
    

    In your view

    <td><%= post.category.categoryname %></td>
    

    You can get rid of the @categories = line in your controller

    Also, categoryname is probably not the best attribute name for your Category model. Why not just name it name. post.category.name seems a lot better than post.category.categoryname, don’t you think?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to install a couple of ways but failed each time. Can
This is very similar to a couple of other questions on stackoverflow, but they
I know there have been a couple questions asked similar to this, but I
I have tried searching over the internet about this problem but not able to
I have tried many permutations but they all don't seems to work well. Am
I have tried a variety of different solutions found on stack and other places
I have asked a couple of similar questions the last couple of days and
I've searched other questions and, while this problem seems similar to a couple of
I have a fairly simple ruby syntax question (and a couple other clarifications), and
I've noticed a couple other questions about handling this, but all seem to suffer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.