ThanksSirAlex
ThanksSirAlex

Reputation: 52

delegate in rails active record

when digging into rails source code there are some some code in activerecord/lib/active_record/querying.rb

QUERYING_METHODS = [
      :find, :find_by, :find_by!, :take, :take!, :first, :first!, :last, :last!,
      :second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!,
      :forty_two, :forty_two!, :third_to_last, :third_to_last!, :second_to_last, :second_to_last!,
      :exists?, :any?, :many?, :none?, :one?,
      :first_or_create, :first_or_create!, :first_or_initialize,
      :find_or_create_by, :find_or_create_by!, :find_or_initialize_by,
      :create_or_find_by, :create_or_find_by!,
      :destroy_all, :delete_all, :update_all, :touch_all, :destroy_by, :delete_by,
      :find_each, :find_in_batches, :in_batches,
      :select, :reselect, :order, :reorder, :group, :limit, :offset, :joins, :left_joins, :left_outer_joins,
      :where, :rewhere, :preload, :extract_associated, :eager_load, :includes, :from, :lock, :readonly, :extending, :or,
      :having, :create_with, :distinct, :references, :none, :unscope, :optimizer_hints, :merge, :except, :only,
      :count, :average, :minimum, :maximum, :sum, :calculate, :annotate,
      :pluck, :pick, :ids
    ].freeze # :nodoc:
    delegate(*QUERYING_METHODS, to: :all)

I can't understand what this to: :all mean since I can't find any class or module named all. So where does these methods go.

Upvotes: 2

Views: 569

Answers (1)

arieljuod
arieljuod

Reputation: 15838

All those methods are a part of an ActiveRecord::Relation. YourModel.all returns a YourModel::ActiveRecord_Relation (which is an ActiveRecord::Relation despite the different name). So, that delegation works a shorthand to do YourModel.all.find without writing .all all the time.

Upvotes: 5

Related Questions