### Home Classes Methods

### Files

- CHANGELOG
- COPYING
- README.rdoc
- advanced_associations.rdoc
- association_basics.rdoc
- cheat_sheet.rdoc
- dataset_basics.rdoc
- dataset_filtering.rdoc
- migration.rdoc
- model_hooks.rdoc
- opening_databases.rdoc
- prepared_statements.rdoc
- querying.rdoc
- reflection.rdoc
- 1.0.txt
- 1.1.txt
- 1.3.txt
- 1.4.0.txt
- 1.5.0.txt
- 2.0.0.txt
- 2.1.0.txt
- 2.10.0.txt
- 2.11.0.txt
- 2.12.0.txt
- 2.2.0.txt
- 2.3.0.txt
- 2.4.0.txt
- 2.5.0.txt
- 2.6.0.txt
- 2.7.0.txt
- 2.8.0.txt
- 2.9.0.txt
- 3.0.0.txt
- 3.1.0.txt
- 3.10.0.txt
- 3.11.0.txt
- 3.12.0.txt
- 3.2.0.txt
- 3.3.0.txt
- 3.4.0.txt
- 3.5.0.txt
- 3.6.0.txt
- 3.7.0.txt
- 3.8.0.txt
- 3.9.0.txt
- sharding.rdoc
- sql.rdoc
- virtual_rows.rdoc

### Class Index

Quicksearch

- Sequel
- Sequel::ADO
- Sequel::ADO::Database
- Sequel::ADO::Dataset
- Sequel::ADO::MSSQL
- Sequel::ADO::MSSQL::DatabaseMethods
- Sequel::ADO::MSSQL::Dataset
- Sequel::AdapterNotFound
- Sequel::Amalgalite
- Sequel::Amalgalite::Database
- Sequel::Amalgalite::Dataset
- Sequel::Amalgalite::SequelTypeMap
- Sequel::BasicObject
- Sequel::BeforeHookFailed
- Sequel::ConnectionPool
- Sequel::ConnectionPool::ClassMethods
- Sequel::DB2
- Sequel::DB2::Database
- Sequel::DB2::Dataset
- Sequel::DBI
- Sequel::DBI::Database
- Sequel::DBI::Dataset
- Sequel::DataObjects
- Sequel::DataObjects::Database
- Sequel::DataObjects::Dataset
- Sequel::DataObjects::MySQL
- Sequel::DataObjects::MySQL::DatabaseMethods
- Sequel::DataObjects::MySQL::Dataset
- Sequel::DataObjects::Postgres
- Sequel::DataObjects::Postgres::AdapterMethods
- Sequel::DataObjects::Postgres::DatabaseMethods
- Sequel::DataObjects::Postgres::Dataset
- Sequel::DataObjects::SQLite
- Sequel::DataObjects::SQLite::DatabaseMethods
- Sequel::DataObjects::SQLite::Dataset
- Sequel::Database
- Sequel::DatabaseConnectionError
- Sequel::DatabaseDisconnectError
- Sequel::DatabaseError
- Sequel::Dataset
- Sequel::Dataset::ArgumentMapper
- Sequel::Dataset::Pagination
- Sequel::Dataset::PreparedStatementMethods
- Sequel::Dataset::QueryBlockCopy
- Sequel::Dataset::StoredProcedureMethods
- Sequel::Dataset::StoredProcedures
- Sequel::Dataset::UnnumberedArgumentMapper
- Sequel::Error
- Sequel::Error::AdapterNotFound
- Sequel::Error::InvalidOperation
- Sequel::Error::InvalidValue
- Sequel::Error::PoolTimeoutError
- Sequel::Error::Rollback
- Sequel::Firebird
- Sequel::Firebird::Database
- Sequel::Firebird::Dataset
- Sequel::Inflections
- Sequel::Informix
- Sequel::Informix::Database
- Sequel::Informix::Dataset
- Sequel::IntegerMigrator
- Sequel::InvalidOperation
- Sequel::InvalidValue
- Sequel::JDBC
- Sequel::JDBC::AS400
- Sequel::JDBC::AS400::DatabaseMethods
- Sequel::JDBC::AS400::Dataset
- Sequel::JDBC::Database
- Sequel::JDBC::Dataset
- Sequel::JDBC::Dataset::PreparedStatementMethods
- Sequel::JDBC::Dataset::StoredProcedureMethods
- Sequel::JDBC::H2
- Sequel::JDBC::H2::DatabaseMethods
- Sequel::JDBC::H2::Dataset
- Sequel::JDBC::JavaLang
- Sequel::JDBC::JavaSQL
- Sequel::JDBC::JavaxNaming
- Sequel::JDBC::MSSQL
- Sequel::JDBC::MSSQL::DatabaseMethods
- Sequel::JDBC::MSSQL::Dataset
- Sequel::JDBC::MySQL
- Sequel::JDBC::MySQL::DatabaseMethods
- Sequel::JDBC::MySQL::Dataset
- Sequel::JDBC::Oracle
- Sequel::JDBC::Oracle::DatabaseMethods
- Sequel::JDBC::Oracle::Dataset
- Sequel::JDBC::Postgres
- Sequel::JDBC::Postgres::AdapterMethods
- Sequel::JDBC::Postgres::DatabaseMethods
- Sequel::JDBC::Postgres::Dataset
- Sequel::JDBC::SQLite
- Sequel::JDBC::SQLite::DatabaseMethods
- Sequel::JDBC::SQLite::Dataset
- Sequel::LiteralString
- Sequel::LooserTypecasting
- Sequel::MSSQL
- Sequel::MSSQL::DatabaseMethods
- Sequel::MSSQL::DatasetMethods
- Sequel::Metaprogramming
- Sequel::Migration
- Sequel::MigrationDSL
- Sequel::Migrator
- Sequel::Migrator::Error
- Sequel::Model
- Sequel::Model::Associations
- Sequel::Model::Associations::AssociationDatasetMethods
- Sequel::Model::Associations::AssociationReflection
- Sequel::Model::Associations::ClassMethods
- Sequel::Model::Associations::DatasetMethods
- Sequel::Model::Associations::InstanceMethods
- Sequel::Model::Associations::ManyToManyAssociationReflection
- Sequel::Model::Associations::ManyToOneAssociationReflection
- Sequel::Model::Associations::OneToManyAssociationReflection
- Sequel::Model::Associations::OneToOneAssociationReflection
- Sequel::Model::ClassMethods
- Sequel::Model::DatasetMethods
- Sequel::Model::Errors
- Sequel::Model::InstanceMethods
- Sequel::MySQL
- Sequel::MySQL::Database
- Sequel::MySQL::DatabaseMethods
- Sequel::MySQL::Dataset
- Sequel::MySQL::Dataset::CallableStatementMethods
- Sequel::MySQL::Dataset::PreparedStatementMethods
- Sequel::MySQL::Dataset::StoredProcedureMethods
- Sequel::MySQL::DatasetMethods
- Sequel::NamedTimezones
- Sequel::NoExistingObject
- Sequel::NotImplemented
- Sequel::ODBC
- Sequel::ODBC::Database
- Sequel::ODBC::Dataset
- Sequel::ODBC::MSSQL
- Sequel::ODBC::MSSQL::DatabaseMethods
- Sequel::ODBC::MSSQL::Dataset
- Sequel::OpenBase
- Sequel::OpenBase::Database
- Sequel::OpenBase::Dataset
- Sequel::Oracle
- Sequel::Oracle::Database
- Sequel::Oracle::DatabaseMethods
- Sequel::Oracle::Dataset
- Sequel::Oracle::DatasetMethods
- Sequel::Plugins
- Sequel::Plugins::ActiveModel
- Sequel::Plugins::ActiveModel::InstanceMethods
- Sequel::Plugins::AssociationDependencies
- Sequel::Plugins::AssociationDependencies::ClassMethods
- Sequel::Plugins::AssociationDependencies::InstanceMethods
- Sequel::Plugins::AssociationPks
- Sequel::Plugins::AssociationPks::ClassMethods
- Sequel::Plugins::AssociationProxies
- Sequel::Plugins::AssociationProxies::AssociationProxy
- Sequel::Plugins::AssociationProxies::ClassMethods
- Sequel::Plugins::BooleanReaders
- Sequel::Plugins::BooleanReaders::ClassMethods
- Sequel::Plugins::Caching
- Sequel::Plugins::Caching::ClassMethods
- Sequel::Plugins::Caching::InstanceMethods
- Sequel::Plugins::ClassTableInheritance
- Sequel::Plugins::ClassTableInheritance::ClassMethods
- Sequel::Plugins::ClassTableInheritance::InstanceMethods
- Sequel::Plugins::Composition
- Sequel::Plugins::Composition::ClassMethods
- Sequel::Plugins::Composition::InstanceMethods
- Sequel::Plugins::ForceEncoding
- Sequel::Plugins::ForceEncoding::ClassMethods
- Sequel::Plugins::ForceEncoding::InstanceMethods
- Sequel::Plugins::HookClassMethods
- Sequel::Plugins::HookClassMethods::ClassMethods
- Sequel::Plugins::HookClassMethods::InstanceMethods
- Sequel::Plugins::IdentityMap
- Sequel::Plugins::IdentityMap::ClassMethods
- Sequel::Plugins::IdentityMap::InstanceMethods
- Sequel::Plugins::InstanceFilters
- Sequel::Plugins::InstanceFilters::InstanceMethods
- Sequel::Plugins::InstanceHooks
- Sequel::Plugins::InstanceHooks::InstanceMethods
- Sequel::Plugins::LazyAttributes
- Sequel::Plugins::LazyAttributes::ClassMethods
- Sequel::Plugins::LazyAttributes::InstanceMethods
- Sequel::Plugins::ManyThroughMany
- Sequel::Plugins::ManyThroughMany::ClassMethods
- Sequel::Plugins::ManyThroughMany::ManyThroughManyAssociationReflection
- Sequel::Plugins::NestedAttributes
- Sequel::Plugins::NestedAttributes::ClassMethods
- Sequel::Plugins::NestedAttributes::InstanceMethods
- Sequel::Plugins::OptimisticLocking
- Sequel::Plugins::OptimisticLocking::ClassMethods
- Sequel::Plugins::OptimisticLocking::InstanceMethods
- Sequel::Plugins::RcteTree
- Sequel::Plugins::Schema
- Sequel::Plugins::Schema::ClassMethods
- Sequel::Plugins::Serialization
- Sequel::Plugins::Serialization::ClassMethods
- Sequel::Plugins::Serialization::InstanceMethods
- Sequel::Plugins::Sharding
- Sequel::Plugins::Sharding::ClassMethods
- Sequel::Plugins::Sharding::DatasetMethods
- Sequel::Plugins::Sharding::InstanceMethods
- Sequel::Plugins::SingleTableInheritance
- Sequel::Plugins::SingleTableInheritance::ClassMethods
- Sequel::Plugins::SingleTableInheritance::InstanceMethods
- Sequel::Plugins::SkipCreateRefresh
- Sequel::Plugins::SkipCreateRefresh::InstanceMethods
- Sequel::Plugins::StringStripper
- Sequel::Plugins::StringStripper::InstanceMethods
- Sequel::Plugins::Subclasses
- Sequel::Plugins::Subclasses::ClassMethods
- Sequel::Plugins::TacticalEagerLoading
- Sequel::Plugins::TacticalEagerLoading::DatasetMethods
- Sequel::Plugins::TacticalEagerLoading::InstanceMethods
- Sequel::Plugins::Timestamps
- Sequel::Plugins::Timestamps::ClassMethods
- Sequel::Plugins::Timestamps::InstanceMethods
- Sequel::Plugins::Touch
- Sequel::Plugins::Touch::ClassMethods
- Sequel::Plugins::Touch::InstanceMethods
- Sequel::Plugins::TypecastOnLoad
- Sequel::Plugins::TypecastOnLoad::ClassMethods
- Sequel::Plugins::TypecastOnLoad::InstanceMethods
- Sequel::Plugins::UpdatePrimaryKey
- Sequel::Plugins::UpdatePrimaryKey::ClassMethods
- Sequel::Plugins::UpdatePrimaryKey::InstanceMethods
- Sequel::Plugins::ValidationClassMethods
- Sequel::Plugins::ValidationClassMethods::ClassMethods
- Sequel::Plugins::ValidationClassMethods::ClassMethods::Generator
- Sequel::Plugins::ValidationClassMethods::InstanceMethods
- Sequel::Plugins::ValidationHelpers
- Sequel::Plugins::ValidationHelpers::InstanceMethods
- Sequel::PoolTimeout
- Sequel::Postgres
- Sequel::Postgres::Adapter
- Sequel::Postgres::AdapterMethods
- Sequel::Postgres::Database
- Sequel::Postgres::DatabaseMethods
- Sequel::Postgres::Dataset
- Sequel::Postgres::Dataset::ArgumentMapper
- Sequel::Postgres::Dataset::BindArgumentMethods
- Sequel::Postgres::Dataset::PreparedStatementMethods
- Sequel::Postgres::DatasetMethods
- Sequel::Postgres::DatasetMethods::PreparedStatementMethods
- Sequel::PrettyTable
- Sequel::Progress
- Sequel::Progress::DatabaseMethods
- Sequel::Progress::DatasetMethods
- Sequel::Rollback
- Sequel::SQL
- Sequel::SQL::AliasMethods
- Sequel::SQL::AliasedExpression
- Sequel::SQL::BitwiseMethods
- Sequel::SQL::Blob
- Sequel::SQL::BooleanConstant
- Sequel::SQL::BooleanExpression
- Sequel::SQL::BooleanMethods
- Sequel::SQL::CaseExpression
- Sequel::SQL::Cast
- Sequel::SQL::CastMethods
- Sequel::SQL::ColumnAll
- Sequel::SQL::ComplexExpression
- Sequel::SQL::ComplexExpressionMethods
- Sequel::SQL::Constant
- Sequel::SQL::Constants
- Sequel::SQL::Expression
- Sequel::SQL::Function
- Sequel::SQL::GenericComplexExpression
- Sequel::SQL::GenericExpression
- Sequel::SQL::Identifier
- Sequel::SQL::IdentifierMethods
- Sequel::SQL::InequalityMethods
- Sequel::SQL::JoinClause
- Sequel::SQL::JoinOnClause
- Sequel::SQL::JoinUsingClause
- Sequel::SQL::NegativeBooleanConstant
- Sequel::SQL::NoBooleanInputMethods
- Sequel::SQL::NumericExpression
- Sequel::SQL::NumericMethods
- Sequel::SQL::OrderMethods
- Sequel::SQL::OrderedExpression
- Sequel::SQL::PlaceholderLiteralString
- Sequel::SQL::QualifiedIdentifier
- Sequel::SQL::QualifyingMethods
- Sequel::SQL::SQLArray
- Sequel::SQL::StringConcatenationMethods
- Sequel::SQL::StringExpression
- Sequel::SQL::StringMethods
- Sequel::SQL::Subscript
- Sequel::SQL::SubscriptMethods
- Sequel::SQL::VirtualRow
- Sequel::SQL::Window
- Sequel::SQL::WindowFunction
- Sequel::SQLite
- Sequel::SQLite::Database
- Sequel::SQLite::DatabaseMethods
- Sequel::SQLite::Dataset
- Sequel::SQLite::Dataset::ArgumentMapper
- Sequel::SQLite::Dataset::PreparedStatementMethods
- Sequel::SQLite::DatasetMethods
- Sequel::Schema
- Sequel::Schema::AlterTableGenerator
- Sequel::Schema::Generator
- Sequel::ShardedSingleConnectionPool
- Sequel::ShardedThreadedConnectionPool
- Sequel::SimpleMigration
- Sequel::SingleConnectionPool
- Sequel::ThreadLocalTimezones
- Sequel::ThreadedConnectionPool
- Sequel::TimestampMigrator
- Sequel::Timezones
- Sequel::ValidationFailed
- String
- String::Inflections
- Array
- FalseClass
- Hash
- NilClass
- Numeric
- Object
- PGconn
- PGresult
- Proc
- Symbol
- TrueClass

No matching classes.

# Advanced Associations

Sequel::Model has the most powerful and flexible associations of any ruby ORM.

    "Extraordinary claims require extraordinary proof" - Carl Sagan

## Background: Sequel::Model association options

There are a bunch of advanced association options that are available to handle the other-than-bog-standard cases. First we’ll go over some of the simpler ones:

All associations take a block that can be used to further filter/modify the default dataset. There’s also an :eager_block option if you want to use a different block when eager loading via Dataset#eager. Association blocks are useful for things like:

    Artist.one_to_many :gold_albums, :class=>:Album do |ds|
      ds.filter{|o| o.copies_sold > 500000}
    end

There are a whole bunch of options for changing how the association is eagerly loaded via Dataset#eager_graph: :graph_block, :graph_conditions, :graph_only_conditions, :graph_join_type (and :graph_join_table_* ones for JOINing to the join table in a many_to_many association).

- :graph_join_type - The type of join to do

- :graph_conditions - Additional conditions to put on join (needs to be a hash or array of all two pairs). Automatically assumes unqualified symbols as first element of the pair to be columns of the associated model, and unqualified symbols of the second element of the pair to be columns of the current model.

- :graph_block - A block passed to join_table, allowing you to specify conditions other than equality, or to use OR, or set up any arbitrary condition. The block is passed the associated table alias, current model alias, and array of previous joins.

- :graph_only_conditions - Use these conditions instead of the standard association conditions. This is necessary when you don’t want to have an equal condition between the foreign key and primary key of the tables. You can also use this to have a JOIN USING (array of symbols), or a NATURAL or CROSS JOIN (nil, with the appropriate :graph_join_type).

These can be used like this:

    # Makes Artist.eager_graph(:required_albums).all not return artists that
    # don't have any albums
    Artist.one_to_many :required_albums, :class=>:Album, :graph_join_type=>:inner

    # Makes sure all returned albums have the active flag set
    Artist.one_to_many :active_albums, :class=>:Album, \
      :graph_conditions=>{:active=>true}

    # Only returns albums that have sold more than 500,000 copies
    Artist.one_to_many :gold_albums, :class=>:Album, \
      :graph_block=>proc{|j,lj,js| :copies_sold.qualify(j) > 500000}

    # Handles the case where the to tables are associated by a case insensitive name string
    Artist.one_to_many :albums, :key=>:artist_name, \
      :graph_only_conditions=>nil, \
      :graph_block=>proc{|j,lj,js| {:lower.sql_function(artist_name.qualify(j))=>:lower.sql_function(name.qualify(lj))}}

    # Handles the case where both key columns have the name artist_name, and you want to use
    # a JOIN USING
    Artist.one_to_many :albums, :key=>:artist_name, :graph_only_conditions=>[:artist_name]

Remember, using eager_graph is generally only necessary when you need to filter/order based on columns in an associated table, it is recommended to use eager for eager loading if possible.

For lazy loading (e.g. Model.association), the :dataset option can be used to specify an arbitrary dataset (one that uses different keys, multiple keys, joins to other tables, etc.).

For eager loading via eager, the :eager_loader option can be used to specify how to eagerly load a complex association. This is an extremely powerful option. Though it can often be verbose (compared to other things in Sequel), it allows you complete control over how to eagerly load associations for a group of objects.

:eager_loader should be a proc that takes 1 or 3 arguments. If the proc takes one argument, it will be given a hash with the following keys:

:key_hash | A key_hash, described below
:rows | An array of model objects
:associations | A hash of dependent associations to eagerly load
:self | The dataset that is doing the eager loading

If the proc takes three arguments, it gets passed the :key_hash, :rows, and :associations values. The only way to get the :self value is to accept one argument. The 3 argument procs are allowed for backwards compatibility, and it is recommended to use the 1 argument proc format for new code.

Since you are given all of the records, you can do things like filter on associations that are specified by multiple keys, or do multiple queries depending on the content of the records (which would be necessary for polymorphic associations). Inside the :eager_loader proc, you should get the related objects and populate the associations cache for all objects in the array of records. The hash of dependent associations is available for you to cascade the eager loading down multiple levels, but it is up to you to use it. The key_hash is a performance enhancement that is used by the default code and is also available to you. It is a hash with keys being foreign/primary key symbols in the current table, and the values being hashes where the key is foreign/primary key values and values being arrays of current model objects having the foreign/primary key value associated with the key. This is hard to visualize, so I’ll give an example:

    album1 = Album.load(:id=>1, :artist_id=>2)
    album2 = Album.load(:id=>3, :artist_id=>2)
    Album.many_to_one :artist
    Album.one_to_many :tracks
    Album.eager(:band, :tracks).all
    # The key_hash provided to the :eager_loader proc would be:
    {:id=>{1=>[album1], 3=>[album2]}, :artist_id=>{2=>[album1, album2]}}

Using these options, you can build associations Sequel doesn’t natively support, and still be able to use the same eager loading features that you are used to.

## ActiveRecord associations

Sequel supports all of associations that ActiveRecord supports, one way or another. Sometimes this requires more code, as Sequel is a toolkit and not a swiss army chainsaw.

### Association callbacks

Sequel supports the same callbacks that ActiveRecord does on one_to_many and many_to_many associations: :before_add, :before_remove, :after_add, and :after_remove. One many_to_one associations and one_to_one associations (which are one_to_many associations with the :one_to_one option), Sequel supports the :before_set and :after_set callbacks. On all associations, Sequel supports :after_load, which is called after the association has been loaded.

Each of these options can be a Symbol specifying an instance method that takes one argument (the associated object), or a Proc that takes two arguments (the current object and the associated object), or an array of Symbols and Procs. For :after_load with a *_to_many association, the associated object argument is an array of associated objects.

If any of the before callbacks return false, the adding/removing does not happen and it either raises an error (the default), or returns nil (if raise_on_save_failure is false).

### Association extensions

All associations come with an association_dataset method that can be further filtered or otherwise modified:

    class Author < Sequel::Model
      one_to_many :authorships
    end
    Author.first.authorships_dataset.filter{|o| o.number < 10}.first

You can extend a dataset with a module easily with :extend. You can reference the model object that created the association dataset via the dataset’s model_object method, and the related association reflection via the dataset’s association_reflection method:

    module FindOrCreate
      def find_or_create(vals)
        first(vals) || model.create(vals.merge(association_reflection[:key]=>model_object.id))
      end
    end
    class Author < Sequel::Model
      one_to_many :authorships, :extend=>FindOrCreate
    end
    Author.first.authorships_dataset.find_or_create(:name=>'Blah', :number=>10)

### has_many :through associations

many_to_many handles the usual case of a has_many :through with a belongs_to in the associated model. It doesn’t break on the case where the join table is a model table, unlike ActiveRecord’s has_and_belongs_to_many.

ActiveRecord:

    class Author < ActiveRecord::Base
      has_many :authorships
      has_many :books, :through => :authorships
    end

    class Authorship < ActiveRecord::Base
      belongs_to :author
      belongs_to :book
    end

    @author = Author.find :first
    @author.books

Sequel::Model:

    class Author < Sequel::Model
      one_to_many :authorships
      many_to_many :books, :join_table=>:authorships
    end

    class Authorship < Sequel::Model
      many_to_one :author
      many_to_one :book
    end

    @author = Author.first
    @author.books

If you use an association other than belongs_to in the associated model, you’ll have to specify some of the :*key options and write a short method.

ActiveRecord:

    class Firm < ActiveRecord::Base
      has_many :clients
      has_many :invoices, :through => :clients
    end

    class Client < ActiveRecord::Base
      belongs_to :firm
      has_many :invoices
    end

    class Invoice < ActiveRecord::Base
      belongs_to :client
      has_one :firm, :through => :client
    end

    Firm.find(:first).invoices

Sequel::Model:

    class Firm < Sequel::Model
      one_to_many :clients
      many_to_many :invoices, :join_table=>:clients, :right_key=>:id, :right_primary_key=>:client_id
    end

    class Client < Sequel::Model
      many_to_one :firm
      one_to_many :invoices
    end

    class Invoice < Sequel::Model
      many_to_one :client

      def firm
        client.firm if client
      end
    end

    Firm.first.invoices

### Polymorphic Associations

Sequel discourages the use of polymorphic associations, which is the reason they are not supported by default. All polymorphic associations can be made non-polymorphic by using additional tables and/or columns instead of having a column containing the associated class name as a string.

Polymorphic associations break referential integrity and are significantly more complex than non-polymorphic associations, so their use is not recommended unless you are stuck with an existing design that uses them.

If you must use them, look for the sequel_polymorphic plugin, as it makes using polymorphic associations in Sequel about as easy as it is in ActiveRecord. However, here’s how they can be done using Sequel’s custom associations:

ActiveRecord:

    class Asset < ActiveRecord::Base
      belongs_to :attachable, :polymorphic => true
    end

    class Post < ActiveRecord::Base
      has_many :assets, :as => :attachable
    end

    class Note < ActiveRecord::Base
      has_many :assets, :as => :attachable
    end

    @asset.attachable = @post
    @asset.attachable = @note

Sequel::Model:

    class Asset < Sequel::Model
      many_to_one :attachable, :reciprocal=>:assets, \
        :dataset=>(proc do
          klass = attachable_type.constantize
          klass.filter(klass.primary_key=>attachable_id)
        end), \
        :eager_loader=>(proc do |eo|
          id_map = {}
          eo[:rows].each do |asset|
            asset.associations[:attachable] = nil
            ((id_map[asset.attachable_type] ||= {})[asset.attachable_id] ||= []) << asset
          end
          id_map.each do |klass_name, id_map|
            klass = klass_name.constantize
            klass.filter(klass.primary_key=>id_map.keys).all do |attach|
              id_map[attach.pk].each do |asset|
                asset.associations[:attachable] = attach
              end
            end
          end
        end)

      private

      def _attachable=(attachable)
        self[:attachable_id] = (attachable.pk if attachable)
        self[:attachable_type] = (attachable.class.name if attachable)
      end
    end

    class Post < Sequel::Model
      one_to_many :assets, :key=>:attachable_id do |ds|
        ds.filter(:attachable_type=>'Post')
      end

      private

      def _add_asset(asset)
        asset.attachable_id = pk
        asset.attachable_type = 'Post'
        asset.save
      end
      def _remove_asset(asset)
        asset.attachable_id = nil
        asset.attachable_type = nil
        asset.save
      end
      def _remove_all_assets
        Asset.filter(:attachable_id=>pk, :attachable_type=>'Post')\
          .update(:attachable_id=>nil, :attachable_type=>nil)
      end
    end

    class Note < Sequel::Model
      one_to_many :assets, :key=>:attachable_id do |ds|
        ds.filter(:attachable_type=>'Note')
      end

      private

      def _add_asset(asset)
        asset.attachable_id = pk
        asset.attachable_type = 'Note'
        asset.save
      end
      def _remove_asset(asset)
        asset.attachable_id = nil
        asset.attachable_type = nil
        asset.save
      end
      def _remove_all_assets
        Asset.filter(:attachable_id=>pk, :attachable_type=>'Note')\
          .update(:attachable_id=>nil, :attachable_type=>nil)
      end
    end

    @asset.attachable = @post
    @asset.attachable = @note

## More advanced associations

So far, we’ve only shown that Sequel::Model has associations as powerful as ActiveRecord’s. Now we will show how Sequel::Model’s associations are more powerful.

### many_to_one/one_to_many not referencing primary key

This can now be handled easily in Sequel using the :primary_key association option. However, this example shows how the association was possible before the introduction of that option.

Let’s say you have two tables, invoices and clients, where each client is associated with many invoices. However, instead of using the client’s primary key, the invoice is associated to the client by name (this is bad database design, but sometimes you have to play with the cards you are dealt).

    class Client < Sequel::Model
      one_to_many :invoices, :reciprocal=>:client, \
        :dataset=>proc{Invoice.filter(:client_name=>name)}, \
        :eager_loader=>(proc do |eo|
          id_map = {}
          eo[:rows].each do |client|
            id_map[client.name] = client
            client.associations[:invoices] = []
          end
          Invoice.filter(:client_name=>id_map.keys.sort).all do |inv|
            inv.associations[:client] = client = id_map[inv.client_name]
            client.associations[:invoices] << inv
          end
        end)

      private

      def _add_invoice(invoice)
        invoice.client_name = name
        invoice.save
      end
      def _remove_invoice(invoice)
        invoice.client_name = nil
        invoice.save
      end
      def _remove_all_invoices
        Invoice.filter(:client_name=>name).update(:client_name=>nil)
      end
    end

    class Invoice < Sequel::Model
      many_to_one :client, :key=>:client_name, \
        :dataset=>proc{Client.filter(:name=>client_name)}, \
        :eager_loader=>(proc do |eo|
          id_map = eo[:key_hash][:client_name]
          eo[:rows].each{|inv| inv.associations[:client] = nil}
          Client.filter(:name=>id_map.keys).all do |client|
            id_map[client.name].each{|inv| inv.associations[:client] = client}
          end
        end)

      private

      def _client=(client)
        self.client_name = (client.name if client)
      end
    end

### Joining on multiple keys

Let’s say you have two tables that are associated with each other with multiple keys. This can now be handled using Sequel’s built in composite key support for associations:

    # Both of these models have an album_id, number, and disc_number fields.
    # All FavoriteTracks have an associated track, but not all tracks have an
    # associated favorite track

    class Track < Sequel::Model
      many_to_one :favorite_track, :key=>[:disc_number, :number, :album_id], :primary_key=>[:disc_number, :number, :album_id]
    end
    class FavoriteTrack < Sequel::Model
      one_to_many :tracks, :key=>[:disc_number, :number, :album_id], :primary_key=>[:disc_number, :number, :album_id], :one_to_one=>true
    end

Here’s the old way to do it via custom associations:

    class Track < Sequel::Model
      many_to_one :favorite_track, \
        :dataset=>(proc do
          FavoriteTrack.filter(:disc_number=>disc_number, :number=>number, :album_id=>album_id)
        end), \
        :eager_loader=>(proc do |eo|
          id_map = {}
          eo[:rows].each do |t|
            t.associations[:favorite_track] = nil
            id_map[[t[:album_id], t[:disc_number], t[:number]]] = t
          end
          FavoriteTrack.filter([:album_id, :disc_number, :number]=>id_map.keys).all do |ft|
            if t = id_map[[ft[:album_id], ft[:disc_number], ft[:number]]]
              t.associations[:favorite_track] = ft
            end
          end
        end)
    end

    class FavoriteTrack < Sequel::Model
      many_to_one :track, \
        :dataset=>(proc do
          Track.filter(:disc_number=>disc_number, :number=>number, :album_id=>album_id)
        end), \
        :eager_loader=>(proc do |eo|
          id_map = {}
          eo[:rows].each{|ft| id_map[[ft[:album_id], ft[:disc_number], ft[:number]]] = ft}
          Track.filter([:album_id, :disc_number, :number]=>id_map.keys).all do |t|
            id_map[[t[:album_id], t[:disc_number], t[:number]]].associations[:track] = t
          end
        end)
    end

### Tree - All Ancestors and Descendents

Let’s say you want to store a tree relationship in your database, it’s pretty simple:

    class Node < Sequel::Model
      many_to_one :parent, :class=>self
      one_to_many :children, :key=>:parent_id, :class=>self
    end

You can easily get a node’s parent with node.parent, and a node’s children with node.children. You can even eager load the relationship up to a certain depth:

    # Eager load three generations of generations of children for a given node
    Node.filter(:id=>1).eager(:children=>{:children=>:children}).all.first
    # Load parents and grandparents for a group of nodes
    Node.filter{|o| o.id < 10}.eager(:parent=>:parent).all

What if you want to get all ancestors up to the root node, or all descendents, without knowing the depth of the tree?

    class Node < Sequel::Model
      many_to_one :ancestors, :class=>self,
       :eager_loader=>(proc do |eo|
        # Handle cases where the root node has the same parent_id as primary_key
        # and also when it is NULL
        non_root_nodes = eo[:rows].reject do |n|
          if [nil, n.pk].include?(n.parent_id)
            # Make sure root nodes have their parent association set to nil
            n.associations[:parent] = nil
            true
          else
            false
          end
        end
        unless non_root_nodes.empty?
          id_map = {}
          # Create an map of parent_ids to nodes that have that parent id
          non_root_nodes.each{|n| (id_map[n.parent_id] ||= []) << n}
          # Doesn't cause an infinte loop, because when only the root node
          # is left, this is not called.
          Node.filter(Node.primary_key=>id_map.keys).eager(:ancestors).all do |node|
            # Populate the parent association for each node
            id_map[node.pk].each{|n| n.associations[:parent] = node}
          end
        end
      end)
      many_to_one :descendants, :eager_loader=>(proc do |eo|
        id_map = {}
        eo[:rows].each do |n|
          # Initialize an empty array of child associations for each parent node
          n.associations[:children] = []
          # Populate identity map of nodes
          id_map[n.pk] = n
        end
        # Doesn't cause an infinite loop, because the :eager_loader is not called
        # if no records are returned.  Exclude id = parent_id to avoid infinite loop
        # if the root note is one of the returned records and it has parent_id = id
        # instead of parent_id = NULL.
        Node.filter(:parent_id=>id_map.keys).exclude(:id=>:parent_id).eager(:descendants).all do |node|
          # Get the parent from the identity map
          parent = id_map[node.parent_id]
          # Set the child's parent association to the parent
          node.associations[:parent] = parent
          # Add the child association to the array of children in the parent
          parent.associations[:children] << node
        end
      end)
    end

Note that unlike ActiveRecord, Sequel supports common table expressions, which allows you to use recursive queries. The results are not the same as in the above case, as all descendents are stored in a single association, but all descendants can be both lazy loaded or eager loaded in a single query (assuming your database supports recursive common table expressions):

    class Node < Sequel::Model
      one_to_many :descendants, :class=>Node, :dataset=>(proc do
          Node.from(:t).
          with_recursive(:t, Node.filter(:parent_id=>pk),
            Node.join(:t, :id=>:parent_id).
            select(:nodes.*))
        end),
        :eager_loader=>(proc do |eo|
          id_map = eo[:key_hash][:id]
          eo[:rows].each{|n| n.associations[:descendants] = []}
          Node.from(:t).
          with_recursive(:t, Node.filter(:parent_id=>id_map.keys).
             select(:parent_id___root, :id, :parent_id),
            Node.join(:t, :id=>:parent_id).
            select(:t__root, :nodes.*)).
          all.each do |node|
            if root = id_map[node.values.delete(:root)].first
              root.associations[:descendants] << node
            end
          end
        end)
    end

Sequel ships with an `rcte_tree` plugin that allows simple creation of ancestors and descendants relationships that use recursive common table expressions:

    Node.plugin :rcte_tree

### Joining multiple keys to a single key, through a third table

Let’s say you have a database, of songs, lyrics, and artists. Each song may or may not have a lyric (most songs are instrumental). The lyric can be associated to an artist in each of four ways: composer, arranger, vocalist, or lyricist. These may all be the same, or they could all be different, and none of them are required. The songs table has a lyric_id field to associate it to the lyric, and the lyric table has four fields to associate it to the artist (composer_id, arranger_id, vocalist_id, and lyricist_id).

What you want to do is get all songs for a given artist, ordered by the song’s name, with no duplicates?

    class Artist < Sequel::Model
      one_to_many :songs, :order=>:songs__name, \
        :dataset=>proc{Song.select(:songs.*).join(Lyric, :id=>:lyric_id, id=>[:composer_id, :arranger_id, :vocalist_id, :lyricist_id])}, \
        :eager_loader=>(proc do |eo|
          h = eo[:key_hash][:id]
          ids = h.keys
          eo[:rows].each{|r| r.associations[:songs] = []}
          Song.select(:songs.*, :lyrics__composer_id, :lyrics__arranger_id, :lyrics__vocalist_id, :lyrics__lyricist_id)\
           .join(Lyric, :id=>:lyric_id){{:composer_id=>ids, :arranger_id=>ids, :vocalist_id=>ids, :lyricist_id=>ids}.sql_or}\
           .order(:songs__name).all do |song|
            [:composer_id, :arranger_id, :vocalist_id, :lyricist_id].each do |x|
              recs = h[song.values.delete(x)]
              recs.each{|r| r.associations[:songs] << song} if recs
            end
          end
          eo[:rows].each{|r| r.associations[:songs].uniq!}
        end)
    end

### Statistics Associations (Sum of Associated Table Column)

In addition to getting associated records, you can use Sequel’s association support to get aggregate information for columns in associated tables (sums, averages, etc.).

Let’s say you have a database with projects and tickets. A project can have many tickets, and each ticket has a number of hours associated with it. You can use the association support to create a Project association that gives the sum of hours for all associated tickets.

    class Project < Sequel::Model
      one_to_many :tickets
      many_to_one :ticket_hours, :read_only=>true, :key=>:id,
       :dataset=>proc{Ticket.filter(:project_id=>id).select{sum(hours).as(hours)}},
       :eager_loader=>(proc do |eo|
        eo[:rows].each{|p| p.associations[:ticket_hours] = nil}
        Ticket.filter(:project_id=>eo[:key_hash][:id].keys).
         group(:project_id).
         select{[project_id, sum(hours).as(hours)]}.
         all do |t|
          p = eo[:key_hash][:id][t.values.delete(:project_id)].first
          p.associations[:ticket_hours] = t
         end
       end)
      # The association method returns a Ticket object with a single aggregate
      # sum-of-hours value, but you want it to return an Integer/Float of just the
      # sum of hours, so you call super and return just the sum-of-hours value.
      # This works for both lazy loading and eager loading.
      def ticket_hours
        if s = super
          s[:hours]
        end
      end
    end
    class Ticket < Sequel::Model
      many_to_one :project
    end

Note that it is often better to use a sum cache instead of this approach. You can implement a sum cache using after_create and after_delete hooks, or using a database trigger (the preferred method if you only have to support one database and that database supports triggers).

[Validate]

Generated with the Darkfish Rdoc Generator 2.