Module | Sequel::Plugins::SingleTableInheritance |
In: |
lib/sequel/plugins/single_table_inheritance.rb
|
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.
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.
# File lib/sequel/plugins/single_table_inheritance.rb, line 21 21: def self.configure(model, key) 22: m = model.method(:constantize) 23: model.instance_eval do 24: @sti_key = key 25: @sti_dataset = dataset 26: dataset.row_proc = lambda{|r| (m.call(r[key]) rescue model).load(r)} 27: end 28: end