In this question I've got two tables for the database:
Suppose that inside a group in facebook happen these sequential actions:
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:
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.
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.