2014-09-26 80 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