How to Observe a Habtm relationship?

Railsters:

ActiveRecord::Observer is a cute little class that lets you bundle groups of after_create and before_update calls together. Don't call us we'll call you.

How can it observe a has_and_belongs_to_many relationship? Mechanically, we need to observe changes in the secret models_models table between the two Models.

that's one of the reasons, why many people (like me)
nearly never use habtm directly, but make the effort
to create the intermediate model by hand and use
the :through option for the final definition.

Maybe there is another trick to get this work, but
the model variant is the common problem solver
in this case. the extra work isn't that much and you
get a lot of functionality for it. For example you can
add status flags or other additional data to the model
and have more freedom handling creating & deleting
the relationship records.

+1… the hardest part is coming up with the name of the model, but from there it’s much nicer. :slight_smile:

Brian Hogan wrote:

+1... the hardest part is coming up with the name of the model, but from there it's much nicer. :slight_smile:

Been there done that. But are we admitting a gap in ActiveRecord's contract?

Confer the habtm directive already supports :after_add => :my_method. We discovered this gap by attempting to move :my_method into the common observer...