How to Observe a Habtm relationship?

7 views
Skip to first unread message

Phlip

unread,
Jul 28, 2008, 12:29:52 PM7/28/08
to rubyonra...@googlegroups.com
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.

--
Phlip

Thorsten Müller

unread,
Jul 28, 2008, 12:53:38 PM7/28/08
to Ruby on Rails: Talk
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.

Brian Hogan

unread,
Jul 28, 2008, 12:55:51 PM7/28/08
to rubyonra...@googlegroups.com
+1... the hardest part is coming up with the name of the model, but from there it's much nicer. :)

Phlip

unread,
Jul 28, 2008, 2:09:17 PM7/28/08
to rubyonra...@googlegroups.com
Brian Hogan wrote:

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

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

--
Phlip

Reply all
Reply to author
Forward
0 new messages