[英]Check if a model was modified or created on before_save event
I want to check if a model is being created in the before_save callback of Rails. 我想检查是否在Rails的before_save回调中创建了一个模型。 I also want to check if it has been modified (when updating).
我还想检查它是否已被修改(更新时)。
Thanks 谢谢
You can use new_record?
你可以使用
new_record?
to see if you have a brand new object and changed?
看看你是否有一个全新的对象并
changed?
to see if anything has changed: 看看是否有任何变化:
before_save :pancakes
def pancakes
if new_record?
# Not in the database yet.
elsif changed?
# Already exists but it has unsaved changes.
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.