跳至内容 跳至搜索

Active Record 核心

命名空间
方法
#
A
C
D
E
F
H
I
L
N
P
R
S
V

属性

[R] strict_loading_mode

类公共方法

attributes_for_inspect

指定将在 inspect 方法输出中包含的属性

Post.attributes_for_inspect = [:id, :title]
Post.first.inspect #=> "#<Post id: 1, title: "Hello, World!">"

当设置为 ':all` 时,inspect 将列出记录的所有属性

Post.attributes_for_inspect = :all
Post.first.inspect #=> "#<Post id: 1, title: "Hello, World!", published_at: "2023-10-23 14:28:11 +0000">"
# File activerecord/lib/active_record/core.rb, line 119
class_attribute :attributes_for_inspect, instance_accessor: false, default: :all

configurations()

返回一个完全解析的 ActiveRecord::DatabaseConfigurations 对象。

# File activerecord/lib/active_record/core.rb, line 77
def self.configurations
  @@configurations
end

configurations=(config)

包含数据库配置 - 通常存储在 config/database.yml 中 - 作为 ActiveRecord::DatabaseConfigurations 对象。

例如,以下 database.yml…

development:
  adapter: sqlite3
  database: storage/development.sqlite3

production:
  adapter: sqlite3
  database: storage/production.sqlite3

…将导致 ActiveRecord::Base.configurations 看起来像这样

#<ActiveRecord::DatabaseConfigurations:0x00007fd1acbdf800 @configurations=[
  #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",
    @name="primary", @config={adapter: "sqlite3", database: "storage/development.sqlite3"}>,
  #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbdea90 @env_name="production",
    @name="primary", @config={adapter: "sqlite3", database: "storage/production.sqlite3"}>
]>
# File activerecord/lib/active_record/core.rb, line 71
def self.configurations=(config)
  @@configurations = ActiveRecord::DatabaseConfigurations.new(config)
end

connection_handler()

# File activerecord/lib/active_record/core.rb, line 133
def self.connection_handler
  ActiveSupport::IsolatedExecutionState[:active_record_connection_handler] || default_connection_handler
end

connection_handler=(handler)

# File activerecord/lib/active_record/core.rb, line 137
def self.connection_handler=(handler)
  ActiveSupport::IsolatedExecutionState[:active_record_connection_handler] = handler
end

current_preventing_writes()

返回表示当前防止写入设置的符号。

ActiveRecord::Base.connected_to(role: :reading) do
  ActiveRecord::Base.current_preventing_writes #=> true
end

ActiveRecord::Base.connected_to(role: :writing) do
  ActiveRecord::Base.current_preventing_writes #=> false
end
# File activerecord/lib/active_record/core.rb, line 196
def self.current_preventing_writes
  connected_to_stack.reverse_each do |hash|
    return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(Base)
    return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(connection_class_for_self)
  end

  false
end

current_role()

返回表示当前连接角色的符号。

ActiveRecord::Base.connected_to(role: :writing) do
  ActiveRecord::Base.current_role #=> :writing
end

ActiveRecord::Base.connected_to(role: :reading) do
  ActiveRecord::Base.current_role #=> :reading
end
# File activerecord/lib/active_record/core.rb, line 159
def self.current_role
  connected_to_stack.reverse_each do |hash|
    return hash[:role] if hash[:role] && hash[:klasses].include?(Base)
    return hash[:role] if hash[:role] && hash[:klasses].include?(connection_class_for_self)
  end

  default_role
end

current_shard()

返回表示当前连接分片的符号。

ActiveRecord::Base.connected_to(role: :reading) do
  ActiveRecord::Base.current_shard #=> :default
end

ActiveRecord::Base.connected_to(role: :writing, shard: :one) do
  ActiveRecord::Base.current_shard #=> :one
end
# File activerecord/lib/active_record/core.rb, line 177
def self.current_shard
  connected_to_stack.reverse_each do |hash|
    return hash[:shard] if hash[:shard] && hash[:klasses].include?(Base)
    return hash[:shard] if hash[:shard] && hash[:klasses].include?(connection_class_for_self)
  end

  default_shard
end

destroy_association_async_batch_size

指定 dependent: :destroy_async 关联选项将在单个后台作业中销毁的最大记录数。当为 nil(默认)时,所有依赖记录将在单个后台作业中销毁。如果指定,要销毁的记录将被拆分为多个后台作业。

# File activerecord/lib/active_record/core.rb, line 47
class_attribute :destroy_association_async_batch_size, instance_writer: false, instance_predicate: false, default: nil

destroy_association_async_job()

用于在后台销毁关联的作业类。

# File activerecord/lib/active_record/core.rb, line 27
def self.destroy_association_async_job
  if _destroy_association_async_job.is_a?(String)
    self._destroy_association_async_job = _destroy_association_async_job.constantize
  end
  _destroy_association_async_job
rescue NameError => error
  raise NameError, "Unable to load destroy_association_async_job: #{error.message}"
end

enumerate_columns_in_select_statements

强制在 SELECT 语句中枚举所有列。例如,SELECT first_name, last_name FROM ... 而不是 SELECT * FROM ... 这在应用程序运行时向数据库添加列时避免了 PreparedStatementCacheExpired 错误。

# File activerecord/lib/active_record/core.rb, line 87
class_attribute :enumerate_columns_in_select_statements, instance_accessor: false, default: false

logger

接受符合 Log4r 接口或默认 Ruby Logger 类的日志记录器,然后将其传递给任何新的数据库连接。您可以在 Active Record 模型类或 Active Record 模型实例上调用 logger 来检索此日志记录器。

# File activerecord/lib/active_record/core.rb, line 22
class_attribute :logger, instance_writer: false

new(attributes = nil)

新对象可以实例化为空的(不传递构造参数)或预先设置了属性但尚未保存(传递一个键名与相关表列名匹配的哈希)。在这两种情况下,有效属性键由相关表的列名确定 - 因此您不能有不在表列中的属性。

示例

# Instantiates a single new object
User.new(first_name: 'Jamie')
# File activerecord/lib/active_record/core.rb, line 460
def initialize(attributes = nil)
  @new_record = true
  @attributes = self.class._default_attributes.deep_dup

  init_internals
  initialize_internals_callback

  super

  yield self if block_given?
  _run_initialize_callbacks
end

实例公共方法

<=>(other_object)

允许对对象进行排序

# File activerecord/lib/active_record/core.rb, line 654
def <=>(other_object)
  if other_object.is_a?(self.class)
    to_key <=> other_object.to_key
  else
    super
  end
end

==(comparison_object)

如果 comparison_object 是完全相同的对象,或者 comparison_object 是相同类型并且 self 有一个 ID 并且它等于 comparison_object.id,则返回 true。

请注意,根据定义,新记录与任何其他记录不同,除非其他记录是接收器本身。此外,如果您使用 select 获取现有记录并省略 ID,则需要自行处理,此谓词将返回 false。

还要注意,销毁记录会保留模型实例中的 ID,因此已删除的模型仍然可以比较。

也称为: eql?
# File activerecord/lib/active_record/core.rb, line 620
def ==(comparison_object)
  super ||
    comparison_object.instance_of?(self.class) &&
    primary_key_values_present? &&
    comparison_object.id == id
end

clone

与 Ruby 的 clone 方法相同。这是一个“浅层”副本。请注意,您的属性没有被复制。这意味着修改克隆的属性将修改原始属性,因为它们都将指向相同的属性哈希。如果您需要属性哈希的副本,请使用 dup 方法。

user = User.first
new_user = user.clone
user.name               # => "Bob"
new_user.name = "Joe"
user.name               # => "Joe"

user.object_id == new_user.object_id            # => false
user.name.object_id == new_user.name.object_id  # => true

user.name.object_id == user.dup.name.object_id  # => false
# File activerecord/lib/active_record/core.rb, line 512
    

connection_handler()

# File activerecord/lib/active_record/core.rb, line 739
def connection_handler
  self.class.connection_handler
end

dup

复制的对象没有分配 ID,并且被视为新记录。请注意,这是一个“浅层”副本,因为它仅复制对象的属性,而不是它的关联。 “深度”副本的范围是特定于应用程序的,因此根据其需要由应用程序来实现。dup 方法不会保留时间戳 (created|updated)_(at|on) 和锁定列。

# File activerecord/lib/active_record/core.rb, line 529
    

encode_with(coder)

使用 coder 填充有关此记录的属性,这些属性应被序列化。此方法中定义的 coder 结构保证与传递给 init_with 方法的 coder 结构匹配。

示例

class Post < ActiveRecord::Base
end
coder = {}
Post.new.encode_with(coder)
coder # => {"attributes" => {"id" => nil, ... }}
# File activerecord/lib/active_record/core.rb, line 576
def encode_with(coder)
  self.class.yaml_encoder.encode(@attributes, coder)
  coder["new_record"] = new_record?
  coder["active_record_yaml_version"] = 2
end

eql?(comparison_object)

别名:==

freeze()

克隆并冻结属性哈希,以便即使在已销毁的记录上也能访问关联,但克隆的模型不会被冻结。

# File activerecord/lib/active_record/core.rb, line 643
def freeze
  @attributes = @attributes.clone.freeze
  self
end

frozen?()

如果属性哈希已被冻结,则返回 true

# File activerecord/lib/active_record/core.rb, line 649
def frozen?
  @attributes.frozen?
end

full_inspect()

将记录的所有属性作为格式良好的字符串返回,忽略 .attributes_for_inspect

Post.first.full_inspect
#=> "#<Post id: 1, title: "Hello, World!", published_at: "2023-10-23 14:28:11 +0000">"
# File activerecord/lib/active_record/core.rb, line 763
def full_inspect
  inspect_with_attributes(all_attributes_for_inspect)
end

hash()

委托给 id,以便允许具有相同类型和 id 的两个记录使用类似的东西

[ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
# File activerecord/lib/active_record/core.rb, line 630
def hash
  id = self.id

  if primary_key_values_present?
    self.class.hash ^ id.hash
  else
    super
  end
end

init_with(coder, &block)

coder 初始化一个空的模型对象。coder 应该是先前使用 encode_with 编码 Active Record 模型的结果。

class Post < ActiveRecord::Base
end

old_post = Post.new(title: "hello world")
coder = {}
old_post.encode_with(coder)

post = Post.allocate
post.init_with(coder)
post.title # => 'hello world'
# File activerecord/lib/active_record/core.rb, line 487
def init_with(coder, &block)
  coder = LegacyYamlAdapter.convert(coder)
  attributes = self.class.yaml_encoder.decode(coder)
  init_with_attributes(attributes, coder["new_record"], &block)
end

inspect()

将记录的属性作为格式良好的字符串返回。

Post.first.inspect
#=> "#<Post id: 1, title: "Hello, World!", published_at: "2023-10-23 14:28:11 +0000">"

属性可以通过设置 .attributes_for_inspect 来限制。

Post.attributes_for_inspect = [:id, :title]
Post.first.inspect
#=> "#<Post id: 1, title: "Hello, World!">"
# File activerecord/lib/active_record/core.rb, line 753
def inspect
  inspect_with_attributes(attributes_for_inspect)
end

pretty_print(pp)

接受一个 PP 并将其美观地打印到其中,允许你从 pp record 中获得一个不错的结果,当需要 pp 时。

# File activerecord/lib/active_record/core.rb, line 769
def pretty_print(pp)
  return super if custom_inspect_method_defined?
  pp.object_address_group(self) do
    if @attributes
      attr_names = attributes_for_inspect.select { |name| _has_attribute?(name.to_s) }
      pp.seplist(attr_names, proc { pp.text "," }) do |attr_name|
        attr_name = attr_name.to_s
        pp.breakable " "
        pp.group(1) do
          pp.text attr_name
          pp.text ":"
          pp.breakable
          value = attribute_for_inspect(attr_name)
          pp.text value
        end
      end
    else
      pp.breakable " "
      pp.text "not initialized"
    end
  end
end

readonly!()

将此记录标记为只读。

customer = Customer.first
customer.readonly!
customer.save # Raises an ActiveRecord::ReadOnlyRecord
# File activerecord/lib/active_record/core.rb, line 735
def readonly!
  @readonly = true
end

readonly?()

如果记录是只读的,则返回 true

# File activerecord/lib/active_record/core.rb, line 671
def readonly?
  @readonly
end

slice(*methods)

返回给定方法的哈希,其名称作为键,返回值作为值。

topic = Topic.new(title: "Budget", author_name: "Jason")
topic.slice(:title, :author_name)
=> { "title" => "Budget", "author_name" => "Jason" }
# File activerecord/lib/active_record/core.rb, line 583
    

strict_loading!(value = true, mode: :all)

将记录设置为 strict_loading 模式。如果记录尝试延迟加载关联,这将引发错误。

user = User.first
user.strict_loading! # => true
user.address.city
=> ActiveRecord::StrictLoadingViolationError
user.comments.to_a
=> ActiveRecord::StrictLoadingViolationError

参数

  • value - 指定是否启用或禁用严格加载的布尔值。

  • :mode - Symbol 指定严格加载模式。默认为 :all。使用 :n_plus_one_only 模式只会对延迟加载导致 n 加一查询的关联引发错误。

示例

user = User.first
user.strict_loading!(false) # => false
user.address.city # => "Tatooine"
user.comments.to_a # => [#<Comment:0x00...]

user.strict_loading!(mode: :n_plus_one_only)
user.address.city # => "Tatooine"
user.comments.to_a # => [#<Comment:0x00...]
user.comments.first.ratings.to_a
=> ActiveRecord::StrictLoadingViolationError
# File activerecord/lib/active_record/core.rb, line 709
def strict_loading!(value = true, mode: :all)
  unless [:all, :n_plus_one_only].include?(mode)
    raise ArgumentError, "The :mode option must be one of [:all, :n_plus_one_only] but #{mode.inspect} was provided."
  end

  @strict_loading_mode = mode
  @strict_loading = value
end

strict_loading?()

如果记录处于 strict_loading 模式,则返回 true

# File activerecord/lib/active_record/core.rb, line 676
def strict_loading?
  @strict_loading
end

strict_loading_all?()

如果记录使用严格加载且启用了 :all 模式,则返回 true

# File activerecord/lib/active_record/core.rb, line 726
def strict_loading_all?
  @strict_loading_mode == :all
end

strict_loading_n_plus_one_only?()

如果记录使用严格加载且启用了 :n_plus_one_only 模式,则返回 true

# File activerecord/lib/active_record/core.rb, line 721
def strict_loading_n_plus_one_only?
  @strict_loading_mode == :n_plus_one_only
end

values_at(*methods)

返回给定方法返回的值的数组。

topic = Topic.new(title: "Budget", author_name: "Jason")
topic.values_at(:title, :author_name)
=> ["Budget", "Jason"]
# File activerecord/lib/active_record/core.rb, line 598