简体   繁体   中英

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

I'm a little bit stumped here on trying to get the fields for a record that I am about to destroy in rails. I want to send the user an email (Based on the record I am deleting)

I haven't been able to understand exactly what variable would hold this information.

I have a document_histories model with fields of sent_to and complaint ID. I want to use that information to send an email to the users affected by the deletion of a record from the document_histories file.

A standard destroy method for me would look like.

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

I was trying to pull the specific document histories with

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

Since delete only deletes one record, I thought that there could be a way of getting thiggs such as @document_history.sent_to. However, I just can't seem to get it dialed in.

destroy方法返回被销毁的对象,所以只需将它捕获到一个变量中,然后用它做你想做的事情。

destroyed_document = @document_history.destroy

The is a pretty common situation, try putting your pre-destroy logic above the destroy method.

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

Also, depending on your Model and if sent_to is a record itself, this will be destroyed along with the other record if you've got something like dependencies: :destroy .

The @document_history that you are deleting still holds the instance in memory. So you can just call @document_history.complaint_id or @document_history.sent_to even after destroying the record.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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