如何从我在rails中删除的记录中获取信息

时间:2016-03-07 20:16:58

标签: ruby-on-rails

我有点难过在试图获取我将要在铁轨中销毁的记录的字段。我想向用户发送一封电子邮件(根据我删除的记录)

我还没有能够准确理解哪个变量会保存这些信息。

我有一个document_histories模型,其中包含sent_to和投诉ID字段。我想使用该信息向受document_histories文件删除记录影响的用户发送电子邮件。

对我来说,标准的破坏方法看起来像。

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

我试图用

来提取特定的文档历史记录

complaint_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之类的thiggs。但是,我似乎无法将其拨入。

3 个答案:

答案 0 :(得分:4)

destroy方法返回被破坏的对象,所以只需在变量中捕获它,然后用它做你想做的事。

destroyed_document = @document_history.destroy

答案 1 :(得分:1)

这是一种非常常见的情况,请尝试将pre-destroy逻辑置于destroy方法之上。

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

此外,根据您的模型,如果sent_to是一个记录本身,如果您有dependencies: :destroy之类的内容,这将与其他记录一起销毁。

答案 2 :(得分:1)

您要删除的@document_history.complaint_id仍将实例保留在内存中。因此,即使在销毁记录后,您也可以致电@document_history.sent_todef 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

vkCreateSwapchainKHR()