我希望能够向用户显示他收到的关于他在我的 rails 应用程序中创建的内容的评论和喜欢,并在另一个用户使用公共活动 gem关注他时通知他。
到目前为止,我已经在我的控制器中创建并包含并为这些对象创建了活动,以创建它们中的Comment
每Like
一个Relationship
。PublicActivity::Common
我的问题是在为当前用户检索这些活动时由他的内容上的活动触发时出现的,到目前为止我有这个:
post_ids = "SELECT post_id FROM posts WHERE user_id = :user_id"
comment_ids = Comment.where("post_id IN (#{post_ids})", user_id: current_user.id).map(&:id)
comment_activities = PublicActivity::Activity.order("created_at desc").where(trackable_id: comment_ids , trackable_type: "Comment")
like_ids = Like.where("post_id IN (#{post_ids})", user_id: current_user.id).map(&:id)
like_activities = PublicActivity::Activity.order("created_at desc").where(trackable_id: like_ids , trackable_type: "Like")
relationship_ids = Relationship.where(followed_id: current_user.id).map(&:id)
relationship_activities = PublicActivity::Activity.order("created_at desc").where(trackable_id: relationship_ids, trackable_type: "Relationship")
@activities = comment_activities + like_activities + relationship_activities
但我觉得这是一种非常复杂的方法,我可能错过了一种更简单的方法,有人有什么建议吗?