Last Updated: February 25, 2016
·
1.648K
· edelpero

Avoid using Callbacks and Observers

I'm not going to point with my finger, but someone at my company (the project manager) tought that was a good idea to create a record of Y model from an after_create callback of X model. The problem was really obvious and came up when we were trying to test some methods of a class related to Y model.

after_* callbacks on Rails models seem to have some of the most tightly-coupled code, especially when it comes to models with associations. Use a callback ONLY when the logic refers to state internal to the object.

Callbacks and Observers will make your classes more difficult to tests most of times. And one thing is for sure, if it's difficult to test, most of times, you're doing something wrong.

Check the source for more detailed examples and explanation.

Source: http://samuelmullen.com/2013/05/the-problem-with-rails-callbacks/