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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:24:51+00:00 2026-06-17T12:24:51+00:00

In this question I’ve got two tables for the database: Post (has_many :comments) ActivityFeed

  • 0

In this question I’ve got two tables for the database:

  • Post (has_many :comments)
  • ActivityFeed (belongs_to item, :polymorphic)
  • Comment (belongs_to :post)

Suppose that inside a group in facebook happen these sequential actions:

  1. User A posted a status “Status 1”
  2. User B posted a status “Status 2”

and then each one of these 2 posts has triggered the creation of a FeedItem (Activity) for the group.

Now when the user loads the page, the feed activities are sorted by created_at/updated_at.
The feed activities are a polymorphic set of objects (can be post, photo, poll, file…).

If in a second moment User A comments the post of user B, the correct order for the feed should now be:

  1. “Status 2”
  2. “Status 1”

So what should happen when User A comments on a “Status 2”? Should I have to find the related Post (where the comment is being added) and update the timestamp from the activity that the Post is related to?

I need some advices for this. Thanks!


UPDATE

class Comment < ActiveRecord:Base
  belongs_to :post, touch: true
end

class Post < ActiveRecord:Base
  has_many :comments
  act_as_activity -> on_create will create a Activity representing this object
end

class Activity < ActiveRecord:Base
  belongs_to :item, :polymorphic => true
  belongs_to :parent, :polymorphic => true
  belongs_to :user
end

So the feed query will lookup for the activities table, since I can have multiple types of objects in the feed. And the timestamp must be refresh in this table when comment a post, because the query is done in Activity table:

SELECT * FROM Activity 
              ORDER BY updated_at
  • 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-17T12:24:52+00:00Added an answer on June 17, 2026 at 12:24 pm

    Using bdares’ idea of using an after_create callback it might look something like this.

    class Comment
      belongs_to :post, touch: true
      after_create do 
         self.post.activity.last_update_at = Time.now
         self.post.activity.save!
      end
    end
    

    This means that you’re sorting Activity by that last_update_at field.

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

Sidebar

Related Questions

This question is related to my previous post: MySQL - Complicated SUMs inside Query
This question is related to this post but I don't see how I can
This question is related to one I asked the other day which I got
( This question about refactoring F# code got me one down vote, but also
This question applies to two scenarios. (1) The first is having a MDI Form
This question is directly related to this SO question I posed about 15 minutes
This question is kind of a follow up to this question I asked a
This question is really basic. What is the performance difference between removing a UIView
This question is similar in concept to this one , except I see I
This question is about good programming practices and avoiding potential holes. I read Joshua

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.