Active Record 核心
- #
- C
- D
- E
- F
- H
- I
- L
- N
- P
- R
- S
- V
属性
[R] | 严格加载模式 |
类公有方法
配置() 链接
返回完全解析的 ActiveRecord::DatabaseConfigurations
对象
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 77 def self.configurations @@configurations end
配置=(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"}>
]>
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 71 def self.configurations=(config) @@configurations = ActiveRecord::DatabaseConfigurations.new(config) end
connection_handler() 链接
connection_handler=(handler) 链接
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 180 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 143 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 161 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
(默认值)时,所有从属记录都将在单个后台作业中销毁。如果指定,要销毁的记录将拆分为多个后台作业。
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
错误。
来源:显示 | 在 GitHub 上
# 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
类的记录器,然后将其传递给建立的任何新数据库连接。可以通过在活动记录模型类或活动记录模型实例上调用 logger
来检索此记录器。
来源:显示 | 在 GitHub 上
# 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')
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 446 def initialize(attributes = nil) @new_record = true @attributes = self.class._default_attributes.deep_dup init_internals initialize_internals_callback assign_attributes(attributes) if attributes yield self if block_given? _run_initialize_callbacks end
实例公共方法
<=>(other_object) 链接
允许对对象进行排序
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 633 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 在模型实例中,因此已删除的模型仍然可比较。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 599 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
来源:在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 498
connection_handler() 链接
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 713 def connection_handler self.class.connection_handler end
dup 链接
复制的对象没有分配的 id,并且被视为新记录。请注意,这是一个“浅”拷贝,因为它只拷贝对象的属性,而不是它的关联。一个“深”拷贝的范围是特定于应用程序的,因此由应用程序根据其需要来实现。dup 方法不会保留时间戳 (created|updated)_(at|on) 和锁定列。
来源:在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 515
encode_with(coder) 链接
使用此记录中应序列化的属性填充 coder
。在此方法中定义的 coder
结构保证与传递给 init_with
方法的 coder
结构匹配。
示例
class Post < ActiveRecord::Base
end
coder = {}
Post.new.encode_with(coder)
coder # => {"attributes" => {"id" => nil, ... }}
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 555 def encode_with(coder) self.class.yaml_encoder.encode(@attributes, coder) coder["new_record"] = new_record? coder["active_record_yaml_version"] = 2 end
freeze() 链接
克隆并冻结属性哈希,以便即使在销毁的记录中也能访问关联,但克隆的模型不会被冻结。
源代码:显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 622 def freeze @attributes = @attributes.clone.freeze self end
frozen?() 链接
如果属性哈希已冻结,则返回 true
。
源代码:显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 628 def frozen? @attributes.frozen? end
hash() 链接
委托给 id 以允许相同类型和 id 的两个记录使用类似以下内容进行处理
[ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
源代码:显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 609 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
对 ActiveRecord 模型进行编码的结果。
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'
源代码:显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 473 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() 链接
以格式良好的字符串形式返回记录的内容。
源代码:显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 718 def inspect # We check defined?(@attributes) not to issue warnings if the object is # allocated but not initialized. inspection = if defined?(@attributes) && @attributes attribute_names.filter_map do |name| if _has_attribute?(name) "#{name}: #{attribute_for_inspect(name)}" end end.join(", ") else "not initialized" end "#<#{self.class} #{inspection}>" end
pretty_print(pp) 链接
获取一个 PP 并漂亮地将此记录打印到其中,允许你在需要 pp 时从 pp record
中获得一个漂亮的结果。
源代码:显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 736 def pretty_print(pp) return super if custom_inspect_method_defined? pp.object_address_group(self) do if defined?(@attributes) && @attributes attr_names = self.class.attribute_names.select { |name| _has_attribute?(name) } pp.seplist(attr_names, proc { pp.text "," }) do |attr_name| pp.breakable " " pp.group(1) do pp.text attr_name pp.text ":" pp.breakable value = _read_attribute(attr_name) value = inspection_filter.filter_param(attr_name, value) unless value.nil? pp.pp 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
源代码:显示 | 在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 709 def readonly! @readonly = true end
readonly?() 链接
如果记录是只读的,则返回 true
。
slice(*methods) 链接
返回给定方法的哈希,其名称作为键,返回的值作为值。
topic = Topic.new(title: "Budget", author_name: "Jason")
topic.slice(:title, :author_name)
=> { "title" => "Budget", "author_name" => "Jason" }
来源:GitHub
# File activerecord/lib/active_record/core.rb, line 562
strict_loading!(value = true, mode: :all) 链接
将记录设置为严格加载模式。如果记录尝试延迟加载关联,这将引发错误。
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 plus one 查询时才会引发错误。
示例
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 688 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?() 链接
如果记录处于严格加载模式,则返回 true
。
strict_loading_n_plus_one_only?() 链接
如果记录使用严格加载,并且启用了 :n_plus_one_only
模式,则返回 true
。
values_at(*methods) 链接
返回给定方法返回的值的数组。
topic = Topic.new(title: "Budget", author_name: "Jason")
topic.values_at(:title, :author_name)
=> ["Budget", "Jason"]
来源:在 GitHub 上
# File activerecord/lib/active_record/core.rb, line 577