The association_pks plugin adds association_pks and association_pks= instance methods to the model class for each association added. These methods allow for easily returning the primary keys of the associated objects, and easily modifying which objects are associated:
Artist.one_to_many :albums artist = Artist artist.album_pks # [1, 2, 3] artist.album_pks = [2, 4] artist.album_pks # [2, 4]
Note that it uses the singular form of the association name. Also note that the setter both associates to new primary keys not in the assocation and disassociates from primary keys not provided to the method.
This plugin makes modifications directly to the underlying tables, it does not create or return any model objects, and therefore does not call any callbacks. If you have any association callbacks, you probably should not use the setter methods.
If an association uses the :delay_pks option, you can set the associated pks for new objects, and the setting will not be persisted until after the object has been created in the database. Additionally, if an association uses the :delay_pks=>:always option, you can set the associated pks for existing objects, and the setting will not be persisted until after the object has been saved.
SEQUEL5: Starting in Sequel 5, the :delay_pks association option will default to :delay_pks=>:always. If you would like to use the Sequel 4 behavior of having the setter make modifications immediately, explicitly use the :delay_pks=>false option. Additionally, Sequel 5 will not support the current :delay_pks=>true behavior, treating it as :delay_pks=>:always.
By default, if you pass a nil value to the setter, an exception will be raised. You can change this behavior by using the :association_pks_nil association option. If set to :ignore, the setter will take no action if nil is given. If set to :remove, the setter will treat the nil as an empty array, removing the association all currently associated values.
# Make all model subclass *_to_many associations have association_pks # methods (called before loading subclasses) Sequel::Model.plugin :association_pks # Make the Album *_to_many associations have association_pks # methods (called before the association methods) Album.plugin :association_pks