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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:41:52+00:00 2026-05-31T02:41:52+00:00

I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to

  • 0

I have a Post model:

class Post < ActiveRecord::Base
  attr_accessible :title, :content, :tag_names

  belongs_to :user

  has_many :comments, :dependent => :destroy
end

  belongs_to :post, :counter_cache => true
  belongs_to :user
end

and a Comment model:

class Comment < ActiveRecord::Base
  attr_accessible :content, :user_id

  belongs_to :post, :counter_cache => true
  belongs_to :user
end

Right now I know how to sort by date: created_at ASC, created_at DESC.

Now I would like to know, how to order by the date of the last comment and sort by last created otherwise?

Pretty much how StackOverflow does. For instance, if a question gets replied it is placed at the top, if other question is replied this one is placed on the top and so on. Questions are sorted ASC otherwise.

Generated SQL:

Started POST "/posts/30/comments" for 127.0.0.1 at 2012-03-12 11:59:32 +0800
Processing by CommentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"0zGpin7T/1aH/JBomGiqsEYCIgQ04HQWHZ/bIjm7WKs=", "comment"=>{"content"=>"sfsfwfewsdesd"}, "commit"=>"Create Comment", "post_id"=>"30"}
  Post Load (0.3ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", "30"]]
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
   (0.2ms)  begin transaction
  SQL (1.1ms)  INSERT INTO "comments" ("content", "created_at", "post_id", "total_votes", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?)  [["content", "sfsfwfewsdesd"], ["created_at", Mon, 12 Mar 2012 03:59:32 UTC +00:00], ["post_id", 30], ["total_votes", 0], ["updated_at", Mon, 12 Mar 2012 03:59:32 UTC +00:00], ["user_id", 2]]
  Post Load (0.4ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = 30 LIMIT 1
  SQL (0.5ms)  UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) + 1 WHERE "posts"."id" = 30
   (130.3ms)  commit transaction
Redirected to http://localhost:3000/posts/30
Completed 302 Found in 156ms (ActiveRecord: 133.4ms) 
  • 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-05-31T02:41:54+00:00Added an answer on May 31, 2026 at 2:41 am

    First, you need to add another datetime column to your post. Let’s call it content_changed_at or something similar. In a new post it has value of created_at. Then you update it every time a comment gets posted. When you need to display posts, you just sort it by this column.

    Here’s untested code, but it should give you the idea.

    class Post < ActiveRecord::Base
      attr_accessible :title, :content, :tag_names
    
      before_create :init_sort_column
    
      belongs_to :user
    
      has_many :comments, :dependent => :destroy
    
      private 
      def init_sort_column
        self.content_changed_at = self.created_at || Time.now
      end
    end
    
      belongs_to :post, :counter_cache => true
      belongs_to :user
    end
    
    class Comment < ActiveRecord::Base
      attr_accessible :content, :user_id
    
      after_create :update_parent_sort_column
    
      belongs_to :post, :counter_cache => true
      belongs_to :user
    
      private
      def update_parent_sort_column
        if post
          post.content_changed_at = self.created_at
        end
      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 Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names has_many
I have the following models: class EventParticipant < ActiveRecord::Base belongs_to :event has_one :user attr_accessible
I have a Tag model: class Tag < ActiveRecord::Base has_many :taggings, :dependent => :destroy
I have the model Post: class Post < ActiveRecord::Base has_one :location, :dependent => :destroy
I have a typical, Post model: class Post< ActiveRecord::Base validates_presence_of :user_id #Line 1 validates_presence_of
I have the following Models: class Topic < ActiveRecord::Base has_many :posts, :dependent => :destroy
I have a User model, a Post model, and an Interest model. User has_many
I have a model as follows: class EntityTag < ActiveRecord::Base attr_protected :user_id, :post_id, :entity_id

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.