Now I hava a problem,how can I make the callback#after_add receive a reference to the join model in a has_many :through association? my code like this:
class Emergency
has_many :departments, :through => :eme_references, :after_add => Proc.new { |eme_reference| eme_reference.eme_flag = 1}
end
the attribute eme_flag is the model EmeReference's attribute! but in the block ,i get the eme_reference.class is Emergency. I want to set the attribute eme_flag of the model EmeReference. That is my question! cheers!
Presumably Emergency
also has_many :eme_references
in order for the :through
association to work?
In that case, you should be able to attach the callback there:
has_many :eme_references,
:after_add => Proc.new { |emergency, eme_ref| # code here }
The block accepts 2 parameters, the first will be the Emergency
, the 2nd will be the EmeReference
being added.
Perhaps a before_save
callback on EmeReference
can also do what you want in this instance?