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

时间:2014-07-07 21:57:22

标签: ruby-on-rails ruby activerecord gem public-activity

我希望能够向用户显示他在我的rails应用中创建的内容上收到的评论和喜欢,并在另一位用户使用public activity 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

但是我觉得这是一个非常复杂的方法,我可能错过了一个更简单的方法,任何人都有任何建议?

1 个答案:

答案 0 :(得分:1)

对此的解决方案一直盯着我。要解决这个问题,我只需在创建活动时设置活动的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")

这使我能够对当前用户的内容采取行动。