module Sequel::Plugins::LazyAttributes

  1. lib/sequel/plugins/lazy_attributes.rb

The lazy_attributes plugin allows users to easily set that some attributes should not be loaded by default when loading model objects. If the attribute is needed after the instance has been retrieved, a database query is made to retreive the value of the attribute.

This plugin depends on the tactical_eager_loading plugin, and allows you to eagerly load lazy attributes for all objects retrieved with the current object. So the following code should issue one query to get the albums and one query to get the reviews for all of those albums:

Album.plugin :lazy_attributes, :review
Album.where{id < 100}.all do |a|
  a.review
end

# You can specify multiple columns to lazily load:
Album.plugin :lazy_attributes, :review, :tracklist

Note that by default on databases that supporting RETURNING, using explicit column selections will cause instance creations to use two queries (insert and refresh) instead of a single query using RETURNING. You can use the insert_returning_select plugin to automatically use RETURNING for instance creations for models using the lazy_attributes plugin.

Methods

Public Class

  1. apply
  2. configure

Public Class methods

apply(model, *attrs)

Lazy attributes requires the tactical_eager_loading plugin

[show source]
   # File lib/sequel/plugins/lazy_attributes.rb
31 def self.apply(model, *attrs)
32   model.plugin :tactical_eager_loading  
33 end
configure(model, *attrs)

Set the attributes given as lazy attributes

[show source]
   # File lib/sequel/plugins/lazy_attributes.rb
36 def self.configure(model, *attrs)
37   model.lazy_attributes(*attrs) unless attrs.empty?
38 end