Module: Sequel::Plugins::SkipSavingColumns

Defined in:
lib/sequel/plugins/skip_saving_columns.rb

Overview

The skip_saving_columms plugin allows skipping specific columns when saving. By default, it skips columns that the database schema indicates are generated columns:

# Assume id column, name column, and id2 generated column
album = Album[1]
album.id # => 1
album.name # => 'X'
album.id2 # => 2
album.save
# UPDATE album SET name = 'X' WHERE (id = 1)

You can override which columns will be skipped:

Album.skip_saving_columns = [:name]
album.save
# UPDATE album SET id2 = 2 WHERE (id = 1)

The skipping happens for all usage of Model#save and callers of it (e.g. Model.create, Model.update). When using the plugin, the only way to get it to save a column marked for skipping is to explicitly specify it:

album.save(columns: [:name, :id2])
album.save
# UPDATE album SET name = 'X', id2 = 2 WHERE (id = 1)

Usage:

# Support skipping saving columns in all Sequel::Model subclasses
# (called before loading subclasses)
Sequel::Model.plugin :skip_saving_columns

# Support skipping saving columns in the Album class
Album.plugin :skip_saving_columns

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.configure(mod) ⇒ Object

Setup skipping of the generated columns for a model with an existing dataset.



41
42
43
44
45
# File 'lib/sequel/plugins/skip_saving_columns.rb', line 41

def self.configure(mod)
  mod.instance_exec do
    set_skip_saving_generated_columns if @dataset
  end
end