Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:检查哪个方法启动了内部保存:在保存之前_Ruby On Rails_Activerecord_Model_Crud - Fatal编程技术网

Ruby on rails Rails:检查哪个方法启动了内部保存:在保存之前

Ruby on rails Rails:检查哪个方法启动了内部保存:在保存之前,ruby-on-rails,activerecord,model,crud,Ruby On Rails,Activerecord,Model,Crud,我想运行一个方法,跟踪CRUD中记录属性的更改,但仅当这些更改与更新操作一起保存时。我现在在模型中有这个: before_save :check_changed def check_changed puts "Period Contributions Changed? : #{period_contributions_changed?}" puts "Total Contributions Changed? : #{total_contributions_changed?}" put

我想运行一个方法,跟踪CRUD中记录属性的更改,但仅当这些更改与更新操作一起保存时。我现在在模型中有这个:

before_save :check_changed
def check_changed
  puts "Period Contributions Changed? : #{period_contributions_changed?}"
  puts "Total Contributions Changed? : #{total_contributions_changed?}"
  puts "Period Expenditures Changed? : #{period_expenditures_changed?}"
  puts "Total Expenditures Changed? : #{total_expenditures_changed?}"
end

但这也取决于新记录的创造。我是否可以在check_changed方法中执行测试,以确定记录是否已创建或更新?谢谢

查看条件回调:如果和:除非

before_save :check_changed, unless: :new_record?
或者您可以在方法中使用“新记录”

def check_changed

  return if new_record?

  puts "Period Contributions Changed? : #{period_contributions_changed?}"
  puts "Total Contributions Changed? : #{total_contributions_changed?}"
  puts "Period Expenditures Changed? : #{period_expenditures_changed?}"
  puts "Total Expenditures Changed? : #{total_expenditures_changed?}"
end
编辑:也应该签出

after_save :check_changed, on: [:update]
文件是