Module: Sequel::Plugins::DatasetAssociations

Defined in:

lib/sequel/plugins/dataset_associations.rb

Overview

DatasetAssociations allows you to easily use your model associations via
datasets. For each association you define, it creates a dataset method for
that association that returns a dataset of all objects that are associated
to objects in the current dataset. Here's a simple example:

This works for all of the association types that ship with Sequel,
including ones implemented in other plugins. Most association options that
are supported when eager loading are supported when using a dataset
association. However, it will only work for limited associations or *_one
associations with orders if the database supports window functions.

As the dataset methods return datasets, you can easily chain the methods to
get associated datasets of associated datasets:

For associations that do JOINs, such as many_to_many, note that the
datasets returned by a dataset association method do not do a JOIN by
default (they use a subquery that JOINs). This can cause problems when you
are doing a select, order, or filter on a column in the joined table. In
that case, you should use the :dataset_associations_join option in
the association, which will make sure the datasets returned by the dataset
association methods also use JOINs, allowing such dataset association
methods to work correctly.

Usage:

# Make all model subclasses create association methods for datasetsSequel::Model.plugin:dataset_associations# Make the Album class create association methods for datasetsAlbum.plugin:dataset_associations