2014-09-26 84 views
2

我无法找到任何确凿的文章,显示如何在rails中创建通知。每个人似乎都在谈论Mailboxer,因为这是一个很好的解决方案,但除了它们的一个段落:Notify方法外,它似乎很模糊。我在Rails中创建通知系统

因此,我正在考虑创建一个属于Activity(公共活动Gem)的通知模型,然后在活动模型上使用after_create回调来调用notify方法,然后调用所讨论活动对象的notify方法。

作为图

class Comment 
    include PublicActivity::Model 
     tracked 

    def notify 
      #Loop through all involved users of this modal instance and create a Notify record pointing to the public activity 
     end 
    end 

    class Activity < PublicActivity::Activity # (I presume I can override the gems Activity model in my App?) 
    after_create :notify 

     private 
     def notify 
      #Call the notify function from the model referenced in the activity, in this case, comment 
     end 
    end 

所以当注释模式被调用时,公共活动跟踪它,然后调用回的意见通知方法的通知模式来拯救

通知模型只会包括

user_id, activity_id, read:boolean 

注:我想我最好把一切都出了控制器的,因为我认为,整个事情就在模型中更好地处理。但我愿意接受建议

感谢

回答

0

首先,你需要创建所需的字段通知模型。如果每次用户完成任何活动(活动模型中的新条目)时都要通知管理员,则可以在活动模型的after_create方法中创建通知。

class Activity 
    after_create :notify 

    def notify 
    n = Notification.create(user_id: self.user_id, activity_id: self.id) 
    n.save 
    end 
end 

上面的代码将创建新的活动创建通知表中的条目,与活动ID和用户ID。

更多解释here