What is the big difference between observers and callbacks?

What is the big difference between observers and callbacks? When and where to use which?

+7
ruby-on-rails
source share
3 answers

This is about sharing problems.

Observers allow you to act out code that really does not apply to models. For example, the User model may have a callback that sends an email confirming registration after saving the user record, but you really do not want this code to be in the model because it is not directly related to the purpose of the model.

Observers allow you to have such a clean separation because you don't have all of this callback code in your models. Observers depend on the model (or models), and not vice versa.

+11
source share

For me it is one and the same. But I prefer callback over Observer.

An observer is a callback, but in a separate model.

The callback is inside the model. Thus, you see that there is a callback, and you can crack it.

0
source share

I know a lot about keeping your models reasonable. So when you find that your model is becoming ridiculously long, it may be time to reorganize it to use an observer.

From a strict architectural prisoner of war (as noted in John's answer) we are talking about separation of duties , but if this is a simple thing, I prefer to keep it in the model anyway.

Technically, there is practically no difference.

0
source share

All Articles