When a comment on our site gets destroyed, the after_destroy callback is used to update stats:
after_destroy do |p|
p.topic.update_attribute(:replies, p.replies - 1)
end
I want to be able to do the same thing when a comment gets “soft-deleted”. When a comment is soft-deleted, the comment.visible field is set from 1 to 0.
Is there a way to tell when this happens during the update callbacks? Something like:
after_update do |p|
if p.visible was changed from 1 to 0, then update stats.
p.visible.update_attribute(:replies, p.replies - 1)
end
end
You can do
p.visible_changed?to see if it changed. You can also see what it changed from and what it changed to with other methods thatActiveModel::Dirtygives you, which is available by default on allActiveRecords.