【问题标题】:Check if a model was modified or created on before_save event检查模型是否在 before_save 事件中被修改或创建
【发布时间】:2011-12-26 19:08:02
【问题描述】:

我想检查是否在 Rails 的 before_save 回调中创建了模型。 我还想检查它是否已被修改(更新时)。

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3.1 rails-activerecord before-save


    【解决方案1】:

    您可以使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 2015-02-10
      • 1970-01-01
      • 2013-12-16
      • 2014-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多