我有一个叫做治疗的课程,这些关联:
class Treatment < ActiveRecord::Base
has_many :threat_treatments, :dependent => :destroy
has_many :threats, :through => :threat_treatments
has_many :subcategory_treatments, :dependent => :destroy
has_many :subcategories, :through => :subcategory_treatments
has_many :treatments_risks, :dependent => :destroy
has_many :risks, :through => :treatments_risks
has_many :risks_from_threats, :through => :threats, :source => :risks
has_many :risks_from_subcategories, :through => :subcategories, :source => :risks
def associate_with_existing_risks
self.risks = self.risks_from_threats & self.risks_from_subcategories
end
end
我需要在创建治疗时自动执行associate_with_existing_risks()方法。如果我使用before_save回调,则两个关联仍未保存且无法访问。如果我使用after_save,因为我需要再次保存处理,我将创建一个无限循环。
我不能使用inverse_of,因为关联是通过关联。
如何以及何时可以执行方法associate_with_existing_risks以在创建治疗时更新风险关联?