Very few people are aware of existence of has_many and habtm association callbacks : before/after_add & before/after_remove as they’re hidden somewhere deep inside documentation. But until now they were very much unusable and buggy. Thanks to bitsweat’s commit of my patch, now we can actually use them :-)
This can be a great step towards our famous Skinny Controller, Fat Model methodology, as these callbacks allow you to move a great amount of logic to models :
1 2 3 4 5 6 7 8 9 10 11 |
class Client < ActiveRecord::Base has_many :employees, :after_add => :assign_project, :after_remove => :reassign_projects def assign_projects(employee) ... end def reassign_projects(employee) ... end end |
These callbacks still may have some room for improvement, please do use them and report back any issues you face with them at Rails Trac and you can add me ( trac username : lifofifo ) to the CC list of you ticket.




