Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我对在Rails中创建通知系统的看法_Ruby On Rails_Notifications_Mailboxer - Fatal编程技术网

Ruby on rails 我对在Rails中创建通知系统的看法

Ruby on rails 我对在Rails中创建通知系统的看法,ruby-on-rails,notifications,mailboxer,Ruby On Rails,Notifications,Mailboxer,我找不到任何关于如何在rails中创建通知的结论性文章。每个人似乎都在谈论Mailboxer,认为这是一个很好的解决方案,但除了他们关于:Notify方法的一段之外,它似乎非常模糊 因此,我正在考虑创建一个属于活动(Public Activity Gem)的通知模型,然后在活动模型上使用after_create回调调用notify方法,该方法随后调用相关活动对象的notify方法 如图所示 class Comment include PublicActivity::Model

我找不到任何关于如何在rails中创建通知的结论性文章。每个人似乎都在谈论Mailboxer,认为这是一个很好的解决方案,但除了他们关于:Notify方法的一段之外,它似乎非常模糊

因此,我正在考虑创建一个属于活动(Public 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
注意:我正在尽我最大的努力使一切远离控制器,因为我认为在模型中处理整个事情会更好。但我愿意接受建议


谢谢

首先,您需要创建一个带有必填字段的通知模型。如果您想在用户每次完成任何活动(活动模型中的新条目)时通知管理员,可以在活动模型上的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

更多的解释

class Activity
  after_create :notify

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