Hacker News new | past | comments | ask | show | jobs | submit login

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?



I 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


They are still maintained, they've just moved to a separate repo/plugin: https://github.com/rails/rails-observers


One 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: