[英]Check if a model was modified or created on before_save event
我想檢查是否在Rails的before_save回調中創建了一個模型。 我還想檢查它是否已被修改(更新時)。
謝謝
你可以使用new_record?
看看你是否有一個全新的對象並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.