3 ms·
What's the recommended alternative to ActiveRecord Observers? I know they aren't perfect, but I wasn't a fan of cluttering my models with lots of after_commit -
by ghiculescu 14y ago
What's the recommended alternative to ActiveRecord Observers? I know they aren't perfect, but I wasn't a fan of cluttering my models with lots of after_commit - is there a better way?
- timdorr 14y agoThey are still maintained, they've just moved to a separate repo/plugin: https://github.com/rails/rails-observers https://github.com/rails/rails-observers
- spohlenz 14y agoOne option would be to use a service object (e.g. UserCreator) which would encapsulate the action (saving a user) along with the action to be taken (delivering a signup email). With external observers, it's not always obvious whether they are enabled or not. Service objects make this more explicit and are easier to test as well.
- byroot 14y agoI don't know if it's recommended but for simple cases where I just want to trigger some methods on multiple models I just use a concern: https://gist.github.com/byroot/5036110 https://gist.github.com/byroot/5036110