Very Important in Associations Callbacks
4.5 Association Callbacks
Normal callbacks hook into the life cycle of Active Record objects, allowing you to work with those objects at various points. For example, you can use a :before_save callback to cause something to happen just before an object is saved.
Association callbacks are similar to normal callbacks, but they are triggered by events in the life cycle of a collection. There are four available association callbacks:
before_addafter_addbefore_removeafter_remove
You define association callbacks by adding options to the association declaration. For example:
Rails passes the object being added or removed to the callback.
You can stack callbacks on a single event by passing them as an array:
If a before_add callback throws an exception, the object does not get added to the collection. Similarly, if a before_remove callback throws an exception, the object does not get removed from the collection.
These callbacks are called only when the associated objects are added or removed through the association collection:
Comments
Post a Comment