Rails的after_save的回调来创建基于column_changed相关联的模式? [英] Rails after_save callback to create an associated model based on column_changed?

查看:210
本文介绍了Rails的after_save的回调来创建基于column_changed相关联的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ActiveRecord模式与状态栏。当模型保存的状态变化,我需要写一个历史文件中的状态变化,谁负责的变化。我在想一个after_save的回调将工作的伟大,但我不能用status_changed?动态方法来确定历史写有必要执行。我不想写的历史,如果该模型被保存,但地位并没有改变。我在处理它现在唯一的想法就是用一个实例变量标志,以确定是否after_save的应该执行。任何想法?

I have an ActiveRecord model with a status column. When the model is saved with a status change I need to write to a history file the change of status and who was responsible for the change. I was thinking an after_save callback would work great, but I can't use the status_changed? dynamic method to determine that the history write is necessary to execute. I don't want to write to the history if the model is saved but the status wasn't changed. My only thought on handling it right now is to use an instance variable flag to determine if the after_save should execute. Any ideas?

推荐答案

这可能已经改变,因为这个问题被张贴,但after_save的回调应该有 * _变化?动态方法可用并正确设置:

This may have changed since the question was posted, but the after_save callback should have the *_changed? dynamic methods available and set correctly:

class Order
  after_save :handle_status_changed, :if => :status_changed?
end

class Order
  after_save :handle_status_changed
  def handle_status_changed
    return unless status_changed?
    ...
  end
end

正常工作对我来说瓦特/ Rails的2.3.2。

Works correctly for me w/ Rails 2.3.2.

这篇关于Rails的after_save的回调来创建基于column_changed相关联的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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