gpt4 book ai didi

mysql - 如何在 Rails 中向所有评论者发送关于多态评论的通知?

转载 作者:行者123 更新时间:2023-12-04 20:02:59 25 4
gpt4 key购买 nike

我正在使用多态关联进行评论。我想向所有对帖子发表评论的用户发送通知。

  • 假设 user1 发布了一个帖子。
  • User2 对此发表了评论。
  • 用户 1 获得通知 (users = User.joins(:posts).where(:posts => {id: params[:post_id]})) “User2 发表了评论
  • 再次,用户 1 评论。
  • 现在,User2 应该会收到通知 (users= ????)
  • 如果 user2、user3、user4 发表评论,则所有相关用户都应收到通知。

comment.rb

class Comment < ApplicationRecord
belongs_to :user
belongs_to :commentable, polymorphic: true
has_many :comments, as: :commentable
validates :comment, presence: true

after_create :notifications
def notifications
#Create Notification
users = ????
(users-[@current_user]).each do |user|
Resque.enqueue(NotifyJob, Notification.create(
recipient: user,
actor: @current_user,
action: "posted",
notifiable: @comment))
end
end

end

用户.rb

 class User < ApplicationRecord
has_many :posts, dependent: :destroy
has_many :comments, as: :commentable, dependent: :destroy
has_many :notifications, foreign_key: :recipient_id, dependent: :destroy
end

post.rb

 class Post < ApplicationRecord
belongs_to :user
validates :comment, presence: true
validates :user_id, presence: true
has_many :comments, as: :commentable
end

comments_controller.rb

module Api 
module V1
class CommentsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :authorize_request
before_action :find_commentable

def new
@comment = Comment.new
end

def create
@comment = @commentable.comments.new(comment_params)
@comment.user = @current_user

if @comment.save
render json: @comment, status: :created

else
render json: { errors: @comment.errors.full_messages },
status: :unprocessable_entity
end
end

def show
@comment = Comment.find(params[:id])
if !@comment.nil?
render json: @comment, status: :ok
else
render json: {errors: @comment.errors.full_messages}, status: :unprocessable_entity
end
end

private

def comment_params
params.permit(:comment)
end

def find_commentable
@commentable = Comment.find_by_id(params[:comment_id]) if params[:comment_id]
@commentable = Post.find_by_id(params[:post_id]) if params[:post_id]
end
end
end
end

最佳答案

您的第一个问题在 User模型。你有

has_many :comments, as: :commentable, dependent: :destroy

但是Comment型号 belongs_to :user并且有 user_id , 这意味着在 User应该是

has_many :comments, dependent: :destroy

在这种情况下,您可以使用 User.first.comments 轻松获取所有用户评论

第二个是回调。 Vyacheslav Loginov 关于将这种复杂逻辑放入 Controller 操作中的不良做法是正确的,但回调也不是好的做法。不确定哪个更糟。您将在每次创建评论时创建通知,甚至是从控制台。通知将在您创建 commnet 的每个测试设置中创建。不确定这是否是您真正想要的。

更好的选择是创建单独的 ServiceObject 来处理通知

class CommentsController < ApplicationController
def create
@comment = @commentable.comments.new(comment_params)
@comment.user = @current_user

if @comment.save
NotificationSender.new(@comment).comment_notification
render json: @comment, status: :created
else
render json: { errors: @comment.errors.full_messages },
status: :unprocessable_entity
end
end
end

class NotificationSender
def initialize(resource)
@resource = resource
end

# you can add here all other notifications as methods
def comment_notification
users = User.where(id: @resource.commentable.comments.pluck(:user_id)).
where.not(id: @resource.user_id)
users.each do |user|
Resque.enqueue(
NotifyJob,
Notification.create(
recipient: user,
actor: @resource.user,
action: "posted",
notifiable: @resource
)
)
end
end
end

关于mysql - 如何在 Rails 中向所有评论者发送关于多态评论的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59174151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com