2016-03-07 84 views
0

我在这里尝试获取我将要在轨道中销毁的记录的字段时有点难住。我想向用户发送一封电子邮件(根据我删除的记录)如何从我在轨道中删除的记录中获取信息

我还没有能够准确了解哪个变量可以保存此信息。

我有一个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 

由于只删除删除一条记录,我认为有可能获得thiggs的一种方式如@ document_history.sent_to。不过,我似乎无法拨打它。

+0

在第一个示例中,“@ document_history”仍将在内存中包含“DocumentHistory”模型。所以你仍然可以访问它的领域。不幸的是,问题在何处停止有意义。你究竟想要做什么? – max

回答

4

destroy方法返回已销毁的对象,所以只需将其捕获到一个变量中,然后按照您的要求进行操作即可。

destroyed_document = @document_history.destroy 
1

这是一种非常常见的情况,请尝试将您的预销毁逻辑置于销毁方法之上。

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

此外,根据你的模型,如果sent_to是记录本身,这将与其他记录一起被破坏,如果你有类似dependencies: :destroy

1

您正在删除的@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 
+0

你确定吗?我将不得不去测试它,因为我清楚地记得这根本不起作用。 – fbelanger

+0

是的,我确定。有关更多信息,请参阅http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-destroy-21。 – Dharam

+0

很酷!谢谢! – fbelanger

相关问题