-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add :eager_loader association option, to specify code to be run when …
…eager loading :eager_loader is powerful option that allows you to completely specify how associations are to be eagerly loaded. It is flexible enough that it can be used to eagerly load association types that Sequel doesn't even natively support, such as polymorphic associations. The eager loading code has been greatly simplified as it now just uses the :eager_loader option, which has defaults that are the same as the previous eager loading behavior. :eager_loader should be a proc that takes 3 arguments, a key_hash, an array of records, and a hash of dependent associations. 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 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]}}
- Loading branch information
1 parent
004b89c
commit a226fad
Showing
4 changed files
with
108 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters