Conditional insertions to hbtm join tables.

Is there anyway I can prevent the collection<<(object, ...) and similar
methods from adding objects that do not satisfy a specific condition.
This would be similar to the :condition option on the
has_and_belongs_to_many association.

I want to somehow place a condition or rewrite
collection<<(object, ...) and similar methods so that when an
"incompatible" object is attempted to be added to the collection, it
returns false or throws an exception. Is this possible?

Thanks!

Sounds like you want validates_associated.

http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M001334

It was a condition relating the record with the association and the
associated record.

I ended up using the :before_add option and raising an exception
(which rolls back the transaction).

Thanks!