简体   繁体   English

删除 post 方法不适用于注意到的 gem rails

[英]delete post method not working with noticed gem rails

i have simple module inquest with comments , I used noticed gem to add notifications to comments , but when I delete inquest post , it stops server with page not working , and delete the whole account , like I can't login with that account again.i'm new with this gem.我有带有评论的简单模块查询,我使用注意到 gem 向评论添加通知,但是当我删除查询帖子时,它会停止服务器,页面不工作,并删除整个帐户,就像我无法再次使用该帐户登录一样。我是这个宝石的新手。 inquest.rb调查.rb

class Inquest < ApplicationRecord
acts_as_votable
belongs_to :user,  dependent: :destroy
has_many :comments, -> {order(:created_at => :desc)}, dependent: :destroy
has_many_attached :images 
validates :description, presence: true
has_noticed_notifications model_name: 'Notification'
has_many :notifications, through: :user, dependent: :destroy
validates :title, :presence => true, :length => { 
    :maximum => 250,
    :minimum => 25,
    :tokenizer => lambda { |str| str.scan(/\w+/) },
    :too_long  => "Please limit your summary to %{count} words"
  }

  end

notification.rb通知.rb

class Notification < ApplicationRecord
 include Noticed::Model
  belongs_to :recipient, polymorphic: true 
 end

comment.rb评论.rb

class Comment < ApplicationRecord
belongs_to :inquest 
belongs_to :user
validates :content, presence: true

  after_create_commit :notify_recipient
 before_destroy :cleanup_notifications
 has_noticed_notifications model_name: 'Notification' 

 private

 def notify_recipient
Commentnotification.with(comment: self, inquest: inquest).deliver_later(inquest.user)
 end

 def cleanup_notifications
   notifications_as_comment.destroy_all
 end
 end

user.rb用户.rb

   has_many :notifications, as: :recipient

commentnotification.rb评论通知.rb

  def message
@inquest = Inquest.find(params[:comment][:inquest_id])
@comment = Comment.find(params[:comment][:id])
@user = User.find(@comment.user_id)
"#{@user.user_name} commented on #{@inquest.title.truncate(10)}"

 end
 def URL
    inquest_path(Inquest.find(params[:comment][:inquest_id]))
 end

The issue is you have dependent: :destroy in your association:问题是你有dependent: :destroy在你的关联中:

class Inquest < ApplicationRecord
  belongs_to :user, dependent: :destroy
end

which deletes the user with Inquest so the account is deleted.使用Inquest删除用户,因此该帐户被删除。 A User can have many Inquest and there are many related associations usually with User so destroying it with other objects is a bad idea.一个User可以有很多Inquest ,并且通常与User有很多相关的关联,所以用其他对象破坏它是一个坏主意。 Instead, the User model should have dependent: :destroy with the associations in general cases.相反,在一般情况下, User模型应该具有dependent: :destroy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM