我有帶有評論的簡單模塊查詢,我使用注意到 gem 向評論添加通知,但是當我洗掉查詢帖子時,它會停止服務器,頁面不作業,并洗掉整個帳戶,就像我無法再次使用該帳戶登錄一樣。我是這個寶石的新手。調查.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
通知.rb
class Notification < ApplicationRecord
include Noticed::Model
belongs_to :recipient, polymorphic: true
end
評論.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
用戶.rb
has_many :notifications, as: :recipient
評論通知.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
uj5u.com熱心網友回復:
問題是您dependent: :destroy在協會中遇到的問題:
class Inquest < ApplicationRecord
belongs_to :user, dependent: :destroy
end
這將洗掉用戶,Inquest因此該帳戶被洗掉。AUser可以有很多Inquest并且通常有很多相關的關聯,User因此與其他物件一起破壞它是一個壞主意。相反,User模型應該dependent: :destroy與一般情況下的關聯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/479267.html
上一篇:ruby3.0.4,redmine5.0.0,rails6.1.4,以webrick開始的測驗安裝失敗,“引數數量錯誤”
下一篇:redmine安裝失敗,ruby3.0.4,mysql2(0.5.4)“未定義方法'split'”“找不到rb_enc_interned_str”
