0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails 5 コードリーディング 第一回 ~ActiveRecord newメソッド編~

Posted at

Railsの使い方はなんとかわかるにようなりました。

でも、コーディング力が足りない。

他の言語に行った時に、応用力がつかないのではないか、、、

と思ったので、

Railsのソースコードを読んでみることにした。

こちらを参考にしてみました。
Railsコードを読んでみた

Active record 周りを読んでみることに
まずは、shift command f で
def new を
全文検索

# 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

(と中略)

    # 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

(途中略)

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

まずは漠然と理解する

新しいものレコードを作るから attribute(属性)はnil
2.
ブロック引数を渡しているが、ブロックはどこにあるのか?
3.
_deprecated_scope_block メソッドが下に定義されているが、意味がわからない
4.
klassとは何か?

疑問は尽きない
もうちょっとさぐってみる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?