Module: Sequel::Plugins::AssociationMultiAddRemove
- Defined in:
- lib/sequel/plugins/association_multi_add_remove.rb
Overview
The association_multi_add_remove plugin allows adding, removing and setting multiple associated objects in a single method call. By default Sequel::Model defines singular add_*
and remove_*
methods that operate on a single associated object, this adds plural forms that operate on multiple associated objects. Example:
artist.albums # => [album1]
artist.add_albums([album2, album3])
artist.albums # => [album1, album2, album3]
artist.remove_albums([album3, album1])
artist.albums # => [album2]
artist.albums = [album2, album3]
artist.albums # => [album2, album3]
It can handle all situations that the normal singular methods handle, but there is no attempt to optimize behavior, so using these methods will not improve performance.
The add/remove/set methods defined by this plugin use a transaction, so if one add/remove/set fails and raises an exception, all adds/removes/set will be rolled back. If you are using database sharding and want to save to a specific shard, call Model#set_server to set the server for this instance, as the transaction will be opened on that server.
You can customize the method names used for adding/removing multiple associated objects using the :multi_add_method and :multi_remove_method association options.
Usage:
# Allow adding/removing/setting multiple associated objects in a single call
# for all model subclass instances (called before loading subclasses):
Sequel::Model.plugin :association_multi_add_remove
# Allow adding/removing/setting multiple associated objects in a single call
# for Album instances (called before defining associations in the class):
Album.plugin :association_multi_add_remove
Defined Under Namespace
Modules: ClassMethods