views:

315

answers:

1

Dear Stack,

Is it possible, using an Observer, to observe the creation of JOIN records? For example, you have a User Model that has_and_belongs_to_many Book Models. Is it possible to monitor books_users records as they are created or deleted or must I have a BookUser model to do this?

Example of what I want to observe:

User.books << book

OR

User.books.push(book)

OR whatever!

Thanks,

Dave K.

+4  A: 

This is the exact reason you should be using a has_many :through, instead of has_and_belongs_to; It allows you to create a BookUser model in which regular activerecord callbacks/observers can be used (such as after_save). This site explains the differences better, http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off

jcapote
I understand completely but the original design called for nothing more than a relationship between the two - therefore no need for a Model. I'll read the link you've provided but my question is only whether or not it;s possible to observe this without a Model? If no, than a Model I will make.
David
You know what though, immediately upon reading this article I've realized that I indeed need has_many, belongs_to - I need a unique id and type to be saved to another table. Thanks for making me think, I was log-jammed.
David