如何确定是否刚刚创建或更新after_save的创纪录 [英] How to determine if a record is just created or updated in after_save

查看:127
本文介绍了如何确定是否刚刚创建或更新after_save的创纪录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在#new_record?函数确定一个记录已被保存。但它始终是假的 after_save的挂钩。有没有一种方法,以确定该记录是否是一个新创建的记录或旧的更新?

The #new_record? function determines if a record has been saved. But it is always false in the after_save hook. Is there a way to determine whether the record is a newly created record or an old one from update?

我希望不要用另一个回调,如 before_create 来设置标志的模型或需要其他查询到数据库。

I'm hoping not to use another callback such as before_create to set a flag in the model or require another query into the db.

任何意见是AP preciated。

Any advice is appreciated.

编辑:需要确定它 after_save的挂钩,为我的特殊用途的情况下,不存在的updated_at updated_on 时间戳

Need to determine it in after_save hook, and for my particular use case, there is no updated_at or updated_on timestamp

推荐答案

我一直在寻找使用一个 after_save的回调。

I was looking to use this for an after_save callback.

有一个简单的解决方案是使用 id_changed?(因为它不会改变更新),甚至 created_at_changed?如果时间戳列是present。

A simpler solution is to use id_changed? (since it won't change on update) or even created_at_changed? if timestamp columns are present.

这篇关于如何确定是否刚刚创建或更新after_save的创纪录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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