[RUBY] Rails 5 Code Reading Part 1 ~ ActiveRecord new Method ~

I've managed to figure out how to use Rails.

But I don't have enough coding ability.

When I go to another language, I don't think I can apply it ...

I thought, so

I decided to read the Rails source code.

I tried to refer to this. I read the Rails code

To read around Active record First, with shift command f def new Full text search

# frozen_string_literal: true

module ActiveRecord
  # = Active Record \Relation
  class Relation
    MULTI_VALUE_METHODS  = [:includes, :eager_load, :preload, :select, :group,
                            :order, :joins, :left_outer_joins, :references,
                            :extending, :unscope, :optimizer_hints, :annotate]

    SINGLE_VALUE_METHODS = [:limit, :offset, :lock, :readonly, :reordering, :strict_loading,
                            :reverse_order, :distinct, :create_with, :skip_query_cache]

    CLAUSE_METHODS = [:where, :having, :from]
    INVALID_METHODS_FOR_DELETE_ALL = [:distinct, :group, :having]

    VALUE_METHODS = MULTI_VALUE_METHODS + SINGLE_VALUE_METHODS + CLAUSE_METHODS

    include Enumerable
    include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain, Delegation

    attr_reader :table, :klass, :loaded, :predicate_builder
    attr_accessor :skip_preloading_value
    alias :model :klass
    alias :loaded? :loaded
    alias :locked? :lock_value

(And omission)

    # Initializes new record from relation while maintaining the current
    # scope.
    #
    # Expects arguments in the same format as {ActiveRecord::Base.new}[rdoc-ref:Core.new].
    #
    #   users = User.where(name: 'DHH')
    #   user = users.new # => #<User id: nil, name: "DHH", created_at: nil, updated_at: nil>
    #
    # You can also pass a block to new with the new record as argument:
    #
    #   user = users.new { |user| user.name = 'Oscar' }
    #   user.name # => Oscar
    def new(attributes = nil, &block)
      block = _deprecated_scope_block("new", &block)
      scoping { klass.new(attributes, &block) }
    end

    alias build new

(Omitted)

def _deprecated_spawn(name)
        spawn.tap { |scope| scope._deprecated_scope_source = name }
      end

      def _deprecated_scope_block(name, &block)
        -> record do
          klass.current_scope = _deprecated_spawn(name)
          yield record if block_given?
        end
      end

    def new(attributes = nil, &block)
      block = _deprecated_scope_block("new", &block)
      scoping { klass.new(attributes, &block) }
    end

    alias build new

First of all, vaguely understand

Since we will create a new record, the attribute is nil 2. I'm passing a block argument, where is the block? 3. The _deprecated_scope_block method is defined below, but I don't understand the meaning 4. What is klass?

The doubts are endless Let's search a little more

Recommended Posts

Rails 5 Code Reading Part 1 ~ ActiveRecord new Method ~
Rails 5 Code Reading Part 2 ~ Action View ~
rails method
[Rails] ActiveRecord
Ruby Gem Code Reading settings logic Part 1
Rails delegate method
Rails Docker ~ Part 1 ~
Rails Docker ~ Part 2 ~
[Rails] Difference between create method and new + save method
[Rails] devise helper method
Rails learning day 2 part 2
static factory method part 1
Rails learning day 1 part 3
Rails learning day 3 part 2
[Rails] devise introduction method
[Rails] ActiveRecord :: Attributes :: ClassMethods
Rails learning Day 1 Part 2
CRuby code reading (2): rb_newobj_of