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 9112129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:39:39+00:00 2026-06-17T03:39:39+00:00

I have a categories table and a posts table. A post belongs to a

  • 0

I have a categories table and a posts table. A post belongs to a category. I want to output a list of all categories with the latest post in that category.

I feel kind of silly, but I’ve been working on this for a few hours now. I’ve tried join and include when querying the categories, but I had problems limiting to only the latest post of each category.

I then tried to create my own hash or array, but I just kept running in to problems. So before I waste anymore time, I thought that the following would be the next cleanest way I can imagine it to work.

I would really appreciate any help I can get on how to achieve this.

The following is my code (stripped to the bare minimum).

db/schema.rb

ActiveRecord::Schema.define(:version => yyyymmddhhmmss) do

  create_table "categories", :force => true do |t|
    t.string   "name"
    t.datetime "created_at",    :null => false
    t.datetime "updated_at",    :null => false
  end

  create_table "posts", :force => true do |t|
    t.string   "title"
    t.integer  "category_id"
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
  end

end

app/models/category.rb

class Category < ActiveRecord::Base
  ...
  has_many :posts
  ...
end

app/models/post.rb

class Post < ActiveRecord::Base
  ...
  belongs_to :category
  ...
end

app/controller/categories_controller.rb

class CategoriesController < ApplicationController
  ...
  def index
    @categories = Category.all
    # The following loop is what my question is about
    @categories.each do |c|
      latest_post = Post.where(:category_id => c.id).order('published_at DESC').first
      # "Inject" post.id and post.title in to the current @categories hash
    end
  end
  ...
end

app/views/categories/index.html.erb

<% @categories.each do |c| %>
  ...
  <h4><a href="<%= category_path(c) %>"><%= c.name %></a></h4>
  # The following line is how I envision the output to work
  <p><a href="<%= post_path(c.post_id) %>"><%= c.post_title %></a></p>
  ...
<% end %>

raise @categories.to_yaml before

---
- !ruby/object:Category
  attributes:
    id: 1
    name: General
    created_at: 2013-01-10 22:08:57.291758000 Z
    updated_at: 2013-01-10 22:09:02.414022000 Z
...

The following is hypothetical.
raise @categories.to_yaml after

---
- !ruby/object:Category
  attributes:
    id: 1
    name: General
    created_at: 2013-01-10 22:08:57.291758000 Z
    updated_at: 2013-01-10 22:09:02.414022000 Z
    post_id: 80
    post_title: Lorem Ipsum
...
  • 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-17T03:39:40+00:00Added an answer on June 17, 2026 at 3:39 am

    Create a one to one association first :

    class Category
      has_one :latest_post, :order => "created_at DESC", class_name => "Post"
    end
    

    and then eager load:

    @categories = Category.includes(:latest_post).all
    

    And… Voilà!

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

Sidebar

Related Questions

I have a Categories table in which each category has a ParentId that can
I have a table categories and table posts . I want to return categories
So I have a query that should get all posts from a table while
I have a table of categories. Each category can either be a root level
I have a database table named Categories . I want to create a form
I have a self referencing table named categories that has a parentcategoryid column that
If I have a table of categories with a featured row that is default
I have a categories table Category [ID, Title] and a Parent/Child table CategoryParent [ID,CategoryID,ParentCategoryID]
Lets say I have 2 tables: blog_posts and categories. Each blog post belongs to
Okay so I have two tables, a category table and a posts table. I

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.