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

  • SEARCH
  • Home
  • 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 8300909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:44:14+00:00 2026-06-08T16:44:14+00:00

I have an articles model in my Rails 3 application. The article model has

  • 0

I have an articles model in my Rails 3 application. The article model has a column called categories which is set using a select box in the new form view. (It’s a select box because the options should never change and there are only four of them).

The index view code I have is as follows:

<% @articles.category.each do |article| %>
 <%= article.category %>
 <% @articles.each do |article| %>
  <tr>
    <td><%= article.title %></td>
    <td><%= article.author %></td>
    <td><%= article.category %></td>    
    <td><%= link_to 'Show', article %></td>
    <td><%= link_to 'Destroy', article, confirm: 'Are you sure?', method: :delete %></td>
  </tr>
 <% end %>
<% end %>

I have grouped by category in my controller:

@articles = Article.group(:category).order('title ASC')

However, this results in an exception which points to the following line <% @articles.category.each do |article| %>.

What is the tidiest (from within my view code) way of achieving the following:

  • Category 1
  • Article 1
  • Article 2
  • Category 2
  • Article 5
  • Category 3
  • Article 8

So each article is listed under it’s category.

  • 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-08T16:44:16+00:00Added an answer on June 8, 2026 at 4:44 pm

    I’d suggest you to use group_by method (Documentation):

    # in your controller
    articles = Article.order('title ASC')
    @grouped_articles = articles.group_by &:category
    
    
    # in your view
    <% @grouped_articles.each do |category, articles| %>
      <%= category %>
      <% articles.each do |a| %>
        <tr>
          <td><%= a.title %></td>
          <td><%= a.author %></td>   
          <td><%= link_to 'Show', a %></td>
          <td><%= link_to 'Destroy', a, confirm: 'Are you sure?', method: :delete %>       </td>
        </tr>
      <% end %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weird design question. I have a model called Article, which has
In my Rails app, I have a model Page which represents one HTML article
I have a controller called articles , which creates the articles model which gets
In my Rails application, I have two models, Articles and Projects, which are both
I have some nested models in my Rails application. i have an article hat
I have a model Articles that has_many Assets which is a polymorphic model that
I have two model: User, Article A user can like or dislike many articles,
I have the following listview : http://www.vogella.de/articles/AndroidListView/article.html the chaper: 8. Tutorial: Domain Model and
I have model Article it has field title with some text that may contain
I'm using Ruby on Rails to develop a web application. I have a wiki

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.