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:
- User A posted a status “Status 1”
- 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:
- “Status 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
Using bdares’ idea of using an after_create callback it might look something like this.
This means that you’re sorting
Activityby thatlast_update_atfield.