Ruby-on-rails 如何在rails中创建通知系统?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15386254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:38:08  来源:igfitidea点击:

How to create notification system in rails?

ruby-on-railsnotificationsgem

提问by cqcn1991

Basically, I want to create a notification like Facebook and Stackoverflow. Specifically, in a Post-Comments system, when a post get commented, everyone involved (people who creates the post and who create comments, except the new commenter) gets a notification message that this post get commented. And the notification get dismissed when people have read it.

基本上,我想创建一个像 Facebook 和 Stackoverflow 这样的通知。具体来说,在 Post-Comments 系统中,当一个帖子被评论时,所有参与的人(创建帖子和创建评论的人,除了新评论者)都会收到一条通知消息,表明该帖子被评论。当人们阅读它时,通知会被取消。

I have tried to use mailboxergem to implement it, but saddly there is no example available using its related methods, including social_streamitself.

我曾尝试使用邮箱gem 来实现它,但遗憾的是没有使用其相关方法的示例,包括social_stream本身。

Is there other way to create the Notification System?

还有其他方法可以创建通知系统吗?

And when I try to create it from scratch I get several problems:

当我尝试从头开始创建它时,我遇到了几个问题:

    Model Notification
    topic_id: integer
    user_id: integer
    checked: boolean #so we can tell whether the notification is read or not
  1. Dismissing the notication after being read by users
  1. 用户阅读后关闭通知

I think we just need to turn every notification messages' "checked" attribute to true after user visit the index of notification.(In the NotificationsController)

我认为我们只需要在用户访问通知索引后将每个通知消息的“已检查”属性设置为 true。(在 NotificationsController 中)

    def index
      @notifications=current_user.notication.all
      @notification.each do |notification|
         notification.checked = true
      end
      @notification.save!
    end

2.Selecting users to notify(and exclude the user making new comment)

2.选择要通知的用户(排除新评论的用户)

I just have no idea in wrting queries....

我只是不知道在编写查询....

3.Creating notifications

3.创建通知

I think this should be something like

我认为这应该是这样的

    #in CommentController
    def create
      #after creating comments, creat notifications
      @users.each do |user|
        Notification.create(topic_id:@topic, user_id: user.id)
      end
    end

But I think this is really ugly

但我觉得这真的很丑

There is no need to anwer the 3 problems above, Any simple solution to the Notification System is preferable , thanks....

以上3个问题无需回答,任何简单的通知系统解决方案都是可取的,谢谢....

采纳答案by Leonel Galán

I think you are on the right path.

我认为你走在正确的道路上。

A slightly better notifications#index

稍微好一点的通知#index

def index
  @notifications = current_user.notications
  @notifications.update_all checked: true
end
  1. Notify this users

    User.uniq.joins(:comments).where(comments: {id: @comment.post.comment_ids}).reject {|user| user == current_user }
    
  1. 通知此用户

    User.uniq.joins(:comments).where(comments: {id: @comment.post.comment_ids}).reject {|user| user == current_user }
    

Unique users that participated in the @comment's post comments, reject (remove from result) current_user.

参与@comment 的帖子评论的唯一用户,拒绝(从结果中删除)current_user。

  1. An observer as pointed out by Jo?o Daniel, it is preferred over an after_create. This "Rails best practice" describes it pretty well: http://rails-bestpractices.com/posts/2010/07/24/use-observer
  1. Jo?o Daniel 指出的观察者,它比 after_create 更受欢迎。这个“Rails 最佳实践”很好地描述了它:http: //rails-bestpractices.com/posts/2010/07/24/use-observer

回答by Remon Amin

there is an amazing gem called public activity ,,you can customize it as you want and here is a screencast about it in railscast http://railscasts.com/episodes/406-public-activityhope that could help you.

有一个很棒的宝石叫做公共活动,你可以根据需要自定义它,这里是 railscast http://railscasts.com/episodes/406-public-activity 中关于它的截屏视频, 希望可以帮助你。

Update

更新

In my rails application I made a similar notifications system like yours to send notifications to all users but in index action you can user

在我的 rails 应用程序中,我制作了一个类似的通知系统来向所有用户发送通知,但在索引操作中,您可以使用

current_user.notifications.update_all(:checked=>true)

and also to send only one notifications to the user once not several times that somebody commented on the post you can use unique_by method

并且只向用户发送一次通知,而不是多次有人对帖子发表评论,您可以使用 unique_by 方法

  @comments [email protected]_by {|a| a[:user_id]}

then you can send notifications to only users of previous comments

然后您可以仅向以前评论的用户发送通知

 @comments.each do |comment|
 comment.user.notifications.create!(....
 end 

hope that could help you

希望能帮到你