- Defined in:
The association_pks plugin adds association_pks, association_pks=, and association_pks_dataset instance methods to the model class for each one_to_many and many_to_many 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_dataset # SELECT id FROM albums WHERE (albums.artist_id = 1) artist.album_pks # [1, 2, 3] artist.album_pks = [2, 4] artist.album_pks # [2, 4] artist.save # Persist changes
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 this plugin adds.
By default, changes to the association will not happen until the object is saved. However, using the delay_pks: false association option, you can have the changes made immediately when the association_pks setter method is called.
By default, repeated calls to the association_pks getter method will not be cached, unless the setter method has been used and the delay_pks: false association option is not used. You can set caching of repeated calls to the association_pks getter method using the :cache_pks association option. You can pass the :refresh option when calling the getter method to ignore any existing cached values, similar to how the :refresh option works with associations.
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.
For many_to_many associations, association_pks assumes the related pks can be accessed directly from the join table. This works in most cases, but in cases where the :right_primary_key association option is used to specify a different primary key in the associated table, association_pks will return the value of the association primary keys (foreign key values to associated table in the join table), not the associated model primary keys. If you would like to use the associated model primary keys, you need to use the :association_pks_use_associated_table association option. If the :association_pks_use_associated_table association option is used, no setter method will be added.
# 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
Defined Under Namespace
Modules: ClassMethods, InstanceMethods