The finder plugin adds Model.finder for defining optimized finder methods. There are two ways to use this. The recommended way is to pass a symbol that represents a model class method that returns a dataset:
def Artist.by_name(name) where(name: name) end Artist.finder :by_name
This creates an optimized first_by_name method, which you can call normally:
Artist.first_by_name("Joe")
The alternative way to use this to pass your own block:
Artist.finder(name: :first_by_name){|pl, ds| ds.where(name: pl.arg).limit(1)}
Additionally, there is a Model.prepared_finder method. This works similarly to Model.finder, but uses a prepared statement. This limits the types of arguments that will be accepted, but can perform better in the database.
Usage:
# Make all model subclasses support Model.finder # (called before loading subclasses) Sequel::Model.plugin :finder # Make the Album class support Model.finder Album.plugin :finder
Classes and Modules
Constants
FINDER_TYPES | = | [:first, :all, :each, :get].freeze |
Public Class methods
apply(model)
[show source]
# File lib/sequel/plugins/finder.rb 38 def self.apply(model) 39 model.instance_exec do 40 @finders ||= {} 41 @finder_loaders ||= {} 42 end 43 end