0
1

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 5 years have passed since last update.

[Ruby on Rails]ActiveRecord::Base の getter/setter

Last updated at Posted at 2019-03-08
app/models/application_record.rb
ApplicationRecord < ActiveRecord::Base
vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record.rb
35 module ActiveRecord
     ...
38   autoload :Base
vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/base.rb
277   class Base
        ...
310     include AttributeMethods
vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/attribute_methods.rb
395    def [](attr_name)
396      read_attribute(attr_name) { |n| missing_attribute(n, caller) }
397    end

       ...

409    def []=(attr_name, value)
410      write_attribute(attr_name, value)
411    end
vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/attribute_methods/read.rb
54      def read_attribute(attr_name, &block)
          name = if self.class.attribute_alias?(attr_name)
            self.class.attribute_alias(attr_name).to_s
          else
            attr_name.to_s
          end

          primary_key = self.class.primary_key
          name = primary_key if name == "id".freeze && primary_key
          sync_with_transaction_state if name == primary_key
          _read_attribute(name, &block)
65      end
vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.1/lib/active_record/attribute_methods/write.rb
35      def write_attribute(attr_name, value)
          name = if self.class.attribute_alias?(attr_name)
            self.class.attribute_alias(attr_name).to_s
          else
            attr_name.to_s
          end

          primary_key = self.class.primary_key
          name = primary_key if name == "id".freeze && primary_key
          sync_with_transaction_state if name == primary_key
          _write_attribute(name, value)
46      end
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?