Ruby on rails Rails:检查哪个方法启动了内部保存:在保存之前
我想运行一个方法,跟踪CRUD中记录属性的更改,但仅当这些更改与更新操作一起保存时。我现在在模型中有这个: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
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]
文件是