You can try lambda as you said, but I'm not sure if this will work. Something like that:
belongs_to :article, :touch => Proc.new{|o| o.article && o.article.public }
According to the implementation , maybe you can try to return nil instead of false in proc when it is not available
belongs_to :article, :touch => Proc.new{|o| o.article && o.article.public ? true : nil }
If this does not work, use before storing the callback as follows:
class Model < ActiveRecord::Base belongs_to :article before_save :touch_public_parent def touch_public_parent article.touch if article && article.public? end end
Let me know if you have any questions.
Update # 1
Relevant part from add_touch_callbacks :
if touch_attribute == true association.touch unless association.nil? else association.touch(touch_attribute) unless association.nil? end
So, if you pass true, then just touch the updated_at attribute. If you pass a field name, it will update that field if you do not pass nil . If you pass nil, nothing updates. That's why I said that maybe you can try the second version of the belongs_to association.
source share