Ruby on rails 提交后激发,但未捕获属性更改
所以我遇到了一些奇怪的事情,需要帮助 我有以下三个模型,它们之间的关系如下所示Ruby on rails 提交后激发,但未捕获属性更改,ruby-on-rails,ruby,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby,Ruby On Rails 4,Activerecord,所以我遇到了一些奇怪的事情,需要帮助 我有以下三个模型,它们之间的关系如下所示 class Assignment < ActiveRecord::Base belongs_to :case_status has_one :application, :through => :case_status, :autosave => false after_create :update_status def update_status self.case_st
class Assignment < ActiveRecord::Base
belongs_to :case_status
has_one :application, :through => :case_status, :autosave => false
after_create :update_status
def update_status
self.case_status.update_attributes!({loan_case_id: update_case_status_id})
end
end
class CaseStatus < ActiveRecord::Base
belongs_to: application
has_many :assignments, :dependent => :destroy
accepts_nested_attributes_for :assignments,
:allow_destroy => true,
reject_if: proc { |attr| attr["lender_id"].blank? && attr["banker_id"].blank? }
after_commit :catch_previous_changes,on: :update
def catch_previous_changes
self.previous_changes
end
end
class Application < ActiveRecord::Base
has_one :case_status, :dependent => :destroy
end
类分配:case_status,:autosave=>false
创建后:更新\u状态
def更新\u状态
self.case\u status.update\u属性!({loan_case_id:update_case_status_id})
结束
结束
类CaseStatus:销毁
接受指定的\u嵌套\u属性\u,
:allow_destroy=>true,
如果:proc{attr|attr[“贷方id”].blank?&attr[“贷方id”].blank?}拒绝
提交后:捕获之前的更改,在::更新
def catch_先前的更改
self.previous\u更改
结束
结束
类应用程序:销毁
结束
基本上,当创建分配时,有一种方法可以更新案例状态模型中该记录的loan\u case\u id
,但是当调用self.versions
代码时,它总是空的
但是,当我在rails控制台上检查记录时,数据已经更新,这使我了解到正在触发after\u commit
调用,但在属性loan\u case\u id
先前更改的线程之前
现在我不知道如何调试它以了解为什么会发生这种情况,因为它只在通过after_create方法完成d更新时发生。当调用
self.previous\u changes
时,更新loan\u case\u id
的任何其他表单始终具有数据 在提交后,您无法从获取任何脏更改,您必须在保存前后使用,类似如下:
class CaseStatus < ActiveRecord::Base
# all other code removed for brevity
around_save :catch_previous_changes, on: :update
def catch_previous_changes
# see what attributes changed
yield
if persisted?
# maybe do so other stuff if everything is ok
end
end
end
class CaseStatus
在提交后,您无法从获得任何脏的更改,您必须在保存前后使用,类似于此:
class CaseStatus < ActiveRecord::Base
# all other code removed for brevity
around_save :catch_previous_changes, on: :update
def catch_previous_changes
# see what attributes changed
yield
if persisted?
# maybe do so other stuff if everything is ok
end
end
end
class CaseStatus