是否有Rails的deleted表,就像在SQL Server? [英] Is there a DELETED table in Rails just as in SQL Server?

查看:189
本文介绍了是否有Rails的deleted表,就像在SQL Server?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够做同样的在我的Rails应用程序展示了下文。正如你所看到的,在SQL服务器有一个名为deleted表,用于存储那些刚刚删除的行。来源:<一href="http://stackoverflow.com/questions/10443829/how-to-create-a-before-delete-trigger-in-sql-server">How建立在SQL Server之前删除触发器?

I need to be able to do the same as demonstrated below in my rails app. As you can see, in SQL server there is a table called DELETED that stores those rows that were just deleted. source: How to create a before delete trigger in SQL Server?

CREATE TRIGGER TRG_AUD_DEL
ON yourTable
FOR DELETE
AS
     INSERT INTO my_audit_table  (col1, col2, ...)
     SELECT col1, col2...
     FROM DELETED 

按照这个逻辑,以轨,有没有这样的表/位置,距离打电话让那些​​只是因为它们是在铁轨删除的行?我知道回调前/ after_destroy但你怎么能访问这些信息正在被破坏?

Applying this logic to rails, is there such a table/location to call from to get those rows just as they are deleted in rails? I am aware of the callback before/after_destroy but how can you access that information that is being destroyed?

推荐答案

依托数据库特有的行为是不是ActiveRecord的是什么。它仅使用一个关系型数据库作为持久存储形式。数据处理,像这样的,被委托给应用程序code。

Relying on database-specific behavior is not what ActiveRecord is about. It solely uses an RDBMS as a form of persistent storage. Data processing, such as this, is delegated to the app code.

ActiveRecord的原则指出,数据库中的每一行是在你的应用程序模型类的一个实例。因此,回调上调用模型的实例,以在短期内销毁。

ActiveRecord principles state that each row in the database is an instance of a model class in your app. Therefore, a callback is called on an instance of a model to be destroyed shortly.

在一个模型被摧毁( before_destroy )的数据应该是通过回调内部类字段访问。所以,你应该能够把这个回调到您的车型之一,你想跟踪:

Before a model is destroyed (before_destroy) your data should be accessible through class fields inside that callback. So you should be able to place this callback into one of your models you wish to keep track of:

def log_to_history
  DeletedStuff.create(
      title: "Something number #{id}",
      deleted_at: Time.now,
      content: self.to_yaml
    )
end

before_delete :log_to_history

这是一个有些奇怪的例子,它允许存储几乎在同一个表中的任何已删除的项目(甚至不同模型的实例),属性是序列化与YAML。但我尽我所能去公开数据的结构,所以它可以很容易地修改,以满足您的需求。

It is a somewhat weird example that allows to store just about any deleted item (even different models' instances) in the same table, attributes are serialized with YAML. But I did my best to expose data structure, so it can be easily altered to suit your needs.

这个例子将需要一个 DeletedStuff 模型领域

This example would require a DeletedStuff model with fields

  • 标题:字符串
  • deleted_at:时间
  • 内容:文字
  • title: string
  • deleted_at:time
  • content:text

这篇关于是否有Rails的deleted表,就像在SQL Server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆