module Sequel::Plugins::Finder

  1. lib/sequel/plugins/finder.rb

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)

Artist.finder :by_name

This creates an optimized first_by_name method, which you can call normally:


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.


# 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


Public Class

  1. apply


FINDER_TYPES = [:first, :all, :each, :get].freeze  

Public Class methods

[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