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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:01:58+00:00 2026-06-05T16:01:58+00:00

I have a Topic model and a Post model. A Topic has many Posts,

  • 0

I have a Topic model and a Post model. A Topic has many Posts, and A Post belongs to Topic.

I want to display index page of Topic based on the most recent Post for each Topic. More recent the last Post is for a Topic, it will be displayed more at the top. I am basically hoping to simulate what regular forums do. If you reply to a topic, the topic rises up in index page.

So I need to somehow order topics based on *created_at* of the last post for each topic. How can this be done?

topics_controller.rb

def index
  @forum = Forum.find(params[:forum_id])
  @topics = @forum.topics.find(:all, :order => "last_post_id DESC")
  # last_post_id is a column of Topic that stores ID of the last post
end

post.rb

class Post < ActiveRecord::Base
  belongs_to :topic
  attr_accessible :content
end

topic.rb

class Topic < ActiveRecord::Base
  has_many :posts, :dependent => :destroy
  belongs_to :forum
  accepts_nested_attributes_for :posts, :allow_destroy => true
  attr_accessible :name, :last_post_id, :posts_attributes
end
  • 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-05T16:01:59+00:00Added an answer on June 5, 2026 at 4:01 pm

    What about Topic.includes(:posts).order(‘posts.created_at DESC’)

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

Sidebar

Related Questions

I have many Topic objects and each Topic hasMany posts:Post How can I order
I have declared models in AppEngine's models.py: class Post(db.Model): topic = db.ReferenceProperty(Topic, collection_name='posts', verbose_name=_('Topic'))
In my project I have a Forum that has many Topics, and each Topic
I'm making a forum in Rails with the traditional Topic has many Posts setup.
I have a url redirect controller called go. It can redirect topic model and
I have this setup in my models: class Author(models.Model): name = models.CharField(max_length=100) class Topic(models.Model):
I have an ActiveRecord Appointment model, which includes topic and time attributes. I would
Hi I read a few post on this topic, lets say I want to
I have this code for creating a topic and post in a forum application
I have a regular master branch, and a topic branch, which has master as

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.