Module: Sequel::Plugins::SingleTableInheritance

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

Overview

Sequel's built in Single Table Inheritance plugin makes subclasses of this model only load rows where the given key field matches the subclass's name. If the key given has a NULL value or there are any problems looking up the class, uses the current class.

You should only use this in the parent class, not in the subclasses.

You shouldn't call set_dataset in the model after applying this plugin, otherwise subclasses might use the wrong dataset.

The filters and row_proc that sti_key sets up in subclasses may not work correctly if those subclasses have further subclasses. For those middle subclasses, you may need to call set_dataset manually with the correct filter and row_proc.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.configure(model, key) ⇒ Object

Set the sti_key and sti_dataset for the model, and change the dataset's row_proc so that the dataset yields objects of varying classes, where the class used has the same name as the key field.


21
22
23
24
25
26
27
28
# File 'lib/sequel/lib/sequel/plugins/single_table_inheritance.rb', line 21

def self.configure(model, key)
  m = model.method(:constantize)
  model.instance_eval do
    @sti_key = key 
    @sti_dataset = dataset
    dataset.row_proc = lambda{|r| (m.call(r[key]) rescue model).load(r)}
  end
end