簡體   English   中英

如何從我在 rails 中刪除的記錄中獲取信息

[英]How can I get the information from a record I am deleting in rails

我在嘗試獲取我即將在 Rails 中銷毀的記錄的字段時有點難住了。 我想給用戶發送一封電子郵件(根據我要刪除的記錄)

我一直無法准確理解哪些變量會保存這些信息。

我有一個帶有 sent_to 和投訴 ID 字段的 document_history 模型。 我想使用該信息向受從 document_history 文件中刪除記錄影響的用戶發送電子郵件。

對我來說標准的銷毀方法看起來像。

  def destroy
    @document_history.destroy
    respond_to do |format|
      format.html
      format.json 
    end
  end

我試圖拉出特定的文檔歷史記錄

投訴_id = $complaint_id

@document_histories = DocumentHistory.where(["complaint_id like ?", "%#{$complaint_id}%"])


@document_histories.each do |hist|
  if hist.complaint_id == $complaint_id
    $was_sent_to = hist.sent_to
  end
end

由於刪除只刪除一條記錄,我認為可能有一種獲取諸如@document_history.sent_to 之類的 thigg 的方法。 但是,我似乎無法撥入它。

destroy方法返回被銷毀的對象,所以只需將它捕獲到一個變量中,然后用它做你想做的事情。

destroyed_document = @document_history.destroy

這是一種很常見的情況,請嘗試將銷毀前的邏輯置於 destroy 方法之上。

def destroy
  respond_to do |format|
    format.html
    format.json 
  end
  @document_history.destroy
end

此外,根據您的模型,如果 sent_to 本身是一條記錄,如果您有dependencies: :destroy類的東西,它將與其他記錄一起dependencies: :destroy

您要刪除的@document_history仍將實例保存在內存中。 因此,即使在銷毀記錄后,您也可以調用@document_history.complaint_id@document_history.sent_to

def destroy
    @document_history.destroy
    # you can still access the attributes
    @document_history.comlaint_id 
    @document_history.sent_to
    respond_to do |format|
      format.html
      format.json 
    end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM