Kulpreet उर्फ Jungly

Urgh.. Why Are People Still Using ActiveRecord Observers?

Came across some source code where models are using observers.

That is kind of OK. If you like them, and they fulfill a need, it is all good.

But if all that the observers are doing are tracking updates to the models they are observing, then are they really needed?

Here’s what I saw recently

1
2
3
4
5
6
7
8
9
10
class Profile < ActiveRecord::Base
end

class ProfileObserver < ActiveRecord::Observer
  observe :notification_profile

    def after_update(profile)
      profile.update_column(:change_attribute, :to_a_value)
  end
end

I would make after_update a filter on Profile.

If someone really wants to separate of concerns and move filters out of the model, then maybe use Rails concerns!