我找不到任何关于如何在rails中创建通知的说明性文章。
因此,我正在考虑创建一个属于Activity (公共活动Gem)的通知模型,然后在活动模型上使用after_create回调来调用notify方法,该方法随后调用所讨论的Activity对象的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因此,当调用注释模式时,公共活动会跟踪它,然后回调注释通知方法以保存在通知模型中
Notify模型仅仅由以下几部分组成
user_id, activity_id, read:boolean 注意:我正在尽我最大的努力将所有事情都排除在控制器之外,因为我认为整个事情在模型中处理会更好。但我对建议持开放态度
谢谢
发布于 2017-10-11 15:28:07
首先,您需要创建一个包含必填字段的Notification模型。如果您希望在用户每次执行任何活动(活动模型中的新条目)时都通知管理员,则可以在活动模型的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。
https://stackoverflow.com/questions/26056168
复制相似问题