繁体   English   中英

使用public_activity gem显示其他用户在当前用户内容上的活动

[英]Displaying activities by other users on current user content with public_activity gem

我希望能够向用户显示他在我的Rails应用程序中创建的内容上的评论和喜欢的内容,并在其他用户使用公共活动gem跟随他时通知他。

到目前为止,我已经使CommentLikeRelationship包含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

但是我觉得这是一种非常复杂的方法,我可能会错过一种更简单的方法,有人有什么建议吗?

嗯,解决这个问题的方法一直都是盯着我。 为了解决这个问题,我只需要在创建活动时设置活动的recipient ,如下所示:

@comment.create_activity :create, owner: current_user, recipient: @post.user

然后获取活动,请执行以下操作:

@activities = PublicActivity::Activity.order("created_at desc").where(recipient_id: current_user.id, recipient_type: "User")

这使我可以对当前用户的内容执行操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM