Ruby-on-rails 如何确定一条记录是刚刚在 after_save 中创建还是更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4975106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine if a record is just created or updated in after_save
提问by Aaron Qian
The #new_record? function determines if a record has been saved. But it is always false in the after_savehook. Is there a way to determine whether the record is a newly created record or an old one from update?
#new_record?函数确定是否已保存记录。但它在after_save钩子里总是假的。有没有办法确定记录是新创建的记录还是更新的旧记录?
I'm hoping not to use another callback such as before_createto set a flag in the model or require another query into the db.
我希望不要使用另一个回调,例如before_create在模型中设置一个标志或需要另一个查询到数据库中。
Any advice is appreciated.
任何建议表示赞赏。
Edit: Need to determine it in after_savehook, and for my particular use case, there is no updated_ator updated_ontimestamp
编辑:需要在after_save钩子中确定它,对于我的特定用例,没有updated_at或updated_on时间戳
回答by Zubin
I was looking to use this for an after_savecallback.
我正在寻找将其用于after_save回调。
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.
一个更简单的解决方案是使用id_changed?(因为它不会改变update)或者即使created_at_changed?存在时间戳列。
Update: As @mitsy points out, if this check is needed outside of callbacks then use id_previously_changed?. See docs.
更新:正如@mitsy 指出的,如果在回调之外需要此检查,则使用id_previously_changed?. 请参阅文档。
回答by Jeff Paquette
No rails magic here that I know of, you'll have to do it yourself. You could clean this up using a virtual attribute...
我知道这里没有任何 Rails 魔法,你必须自己做。您可以使用虚拟属性清理它...
In your model class:
在您的模型类中:
def before_save
@was_a_new_record = new_record?
return true
end
def after_save
if @was_a_new_record
...
end
end
回答by colllin
Yet another option, for those who dohave an updated_attimestamp:
另一种选择,对于那些谁做有一个updated_at时间戳:
if created_at == updated_at
# it's a newly created record
end
回答by sharpone
There is an after_createcallback which is only called if the record is a new record, after it is saved. There is also an after_updatecallback for use if this was an existing record which was changed and saved. The after_savecallback is called in both cases, after either after_createor after_updateis called.
有一个after_create回调,只有在记录是新记录时才会在保存后调用。after_update如果这是已更改和保存的现有记录,则还有一个回调可供使用。该after_save回调被称为在这两种情况下,无论是后after_create还是after_update被调用。
Use after_createif you need something to happen once after a new record has been saved.
after_create如果您需要在保存新记录后发生某事,请使用。
More info here: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
更多信息:http: //api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
回答by Tom Rossi
Since the object has already been saved, you would you need to look at the previous changes. The ID should only change after a create.
由于对象已经保存,您需要查看之前的更改。ID 只应在创建后更改。
# true if this is a new record
@object.previous_changes[:id].any?
There is also an instance variable @new_record_before_save. You can access that by doing the following:
还有一个实例变量@new_record_before_save。您可以通过执行以下操作来访问它:
# true if this is a new record
@object.instance_variable_get(:@new_record_before_save)
Both are pretty ugly, but they would allow you to know whether the object has been newly created. Hope that helps!
两者都很丑陋,但它们可以让您知道对象是否是新创建的。希望有帮助!
回答by Hymana
Rails 5.1+ way:
Rails 5.1+ 方式:
user = User.new
user.save!
user.saved_change_to_attribute?(:id) # => true

