跳至内容 跳至搜索
方法
C
I
L
N
O
P
Q
R
S
T

实例公共方法

column_defaults()

返回一个哈希,其中键是列名,值是在实例化此表对应的 Active Record 对象时的默认值。

# File activerecord/lib/active_record/model_schema.rb, line 488
def column_defaults
  load_schema
  @column_defaults ||= _default_attributes.deep_dup.to_hash.freeze
end

column_for_attribute(name)

返回指定属性的列对象。如果指定的属性不存在,则返回 ActiveRecord::ConnectionAdapters::NullColumn。

class Person < ActiveRecord::Base
end

person = Person.new
person.column_for_attribute(:name) # the result depends on the ConnectionAdapter
# => #<ActiveRecord::ConnectionAdapters::Column:0x007ff4ab083980 @name="name", @sql_type="varchar(255)", @null=true, ...>

person.column_for_attribute(:nothing)
# => #<ActiveRecord::ConnectionAdapters::NullColumn:0xXXX @name=nil, @sql_type=nil, @cast_type=#<Type::Value>, ...>
# File activerecord/lib/active_record/model_schema.rb, line 479
def column_for_attribute(name)
  name = name.to_s
  columns_hash.fetch(name) do
    ConnectionAdapters::NullColumn.new(name)
  end
end

column_names()

返回一个字符串数组,包含列名。

# File activerecord/lib/active_record/model_schema.rb, line 494
def column_names
  @column_names ||= columns.map(&:name).freeze
end

columns()

# File activerecord/lib/active_record/model_schema.rb, line 448
def columns
  @columns ||= columns_hash.values.freeze
end

content_columns()

返回一个列对象数组,其中移除了主键、所有以“_id”或“_count”结尾的列,以及用于单表继承的列。

# File activerecord/lib/active_record/model_schema.rb, line 505
def content_columns
  @content_columns ||= columns.reject do |c|
    c.name == primary_key ||
    c.name == inheritance_column ||
    c.name.end_with?("_id", "_count")
  end.freeze
end

ignored_columns()

模型应忽略的列名列表。忽略的列不会定义属性访问器,也不会在 SQL 查询中引用。

# File activerecord/lib/active_record/model_schema.rb, line 334
def ignored_columns
  @ignored_columns || superclass.ignored_columns
end

ignored_columns=(columns)

设置模型应忽略的列名。忽略的列不会定义属性访问器,也不会在 SQL 查询中引用。

此方法的一个常见用法模式是确保在部署并运行数据库列删除迁移之前,已删除对属性的所有引用并已部署。使用此两步方法删除列可确保在运行架构迁移时,没有代码会因内存中缓存的架构而引发错误。

例如,对于要删除“category”属性的模型,首先将其标记为已忽略

class Project < ActiveRecord::Base
  # schema:
  #   id         :bigint
  #   name       :string, limit: 255
  #   category   :string, limit: 255

  self.ignored_columns += [:category]
end

架构仍然包含“category”,但现在模型已将其省略,因此任何元驱动的代码或架构缓存都不会尝试使用该列

Project.columns_hash["category"] => nil

直接访问该属性将导致错误,因此请确保已删除对该列的所有用法(自动化测试可以帮助您找到任何用法)。

user = Project.create!(name: "First Project")
user.category # => raises NoMethodError
# File activerecord/lib/active_record/model_schema.rb, line 375
def ignored_columns=(columns)
  check_model_columns(@only_columns.present?)
  reload_schema_from_cache
  @ignored_columns = columns.map(&:to_s).freeze
end

load_schema()

从架构缓存或直接从数据库加载模型的架构信息。

# File activerecord/lib/active_record/model_schema.rb, line 550
def load_schema
  return if schema_loaded?
  @load_schema_monitor.synchronize do
    return if schema_loaded?

    load_schema!

    @schema_loaded = true
  rescue
    reload_schema_from_cache # If the schema loading failed half way through, we must reset the state.
    raise
  end
end

next_sequence_value()

返回将在插入语句中用作主键的下一个值。

# File activerecord/lib/active_record/model_schema.rb, line 427
def next_sequence_value
  with_connection { |c| c.next_sequence_value(sequence_name) }
end

only_columns()

模型应允许的列名列表。仅使用这些列来定义属性访问器,并在 SQL 查询中引用它们。

# File activerecord/lib/active_record/model_schema.rb, line 340
def only_columns
  @only_columns || superclass.only_columns
end

only_columns=(columns)

# File activerecord/lib/active_record/model_schema.rb, line 381
def only_columns=(columns)
  check_model_columns(@ignored_columns.present?)
  reload_schema_from_cache
  @only_columns = columns.map(&:to_s).freeze
end

prefetch_primary_key?()

确定在插入语句之前是否应从相应的序列中选择主键值。

# File activerecord/lib/active_record/model_schema.rb, line 421
def prefetch_primary_key?
  with_connection { |c| c.prefetch_primary_key?(table_name) }
end

protected_environments()

禁止执行破坏性操作的环境名称数组。默认值为 ["production"]

# File activerecord/lib/active_record/model_schema.rb, line 315
def protected_environments
  if defined?(@protected_environments)
    @protected_environments
  else
    superclass.protected_environments
  end
end

protected_environments=(environments)

设置禁止执行破坏性操作的环境名称数组。

# File activerecord/lib/active_record/model_schema.rb, line 324
def protected_environments=(environments)
  @protected_environments = environments.map(&:to_s)
end

quoted_table_name()

返回表名的带引号版本。

# File activerecord/lib/active_record/model_schema.rb, line 288
def quoted_table_name
  adapter_class.quote_table_name(table_name)
end

reset_column_information()

重置所有关于列的缓存信息,这将导致它们在下次请求时重新加载。

此方法最常见的用法模式可能是在迁移中,刚创建表后您想用一些默认值填充它,例如:

class CreateJobLevels < ActiveRecord::Migration[8.1]
  def up
    create_table :job_levels do |t|
      t.integer :id
      t.string :name

      t.timestamps
    end

    JobLevel.reset_column_information
    %w{assistant executive manager director}.each do |type|
      JobLevel.create(name: type)
    end
  end

  def down
    drop_table :job_levels
  end
end
# File activerecord/lib/active_record/model_schema.rb, line 539
def reset_column_information
  connection_pool.active_connection&.clear_cache!
  ([self] + descendants).each(&:undefine_attribute_methods)
  schema_cache.clear_data_source_cache!(table_name)

  reload_schema_from_cache
  initialize_find_by_cache
end

sequence_name()

# File activerecord/lib/active_record/model_schema.rb, line 387
def sequence_name
  if base_class?
    @sequence_name ||= reset_sequence_name
  else
    (@sequence_name ||= nil) || base_class.sequence_name
  end
end

sequence_name=(value)

将用于生成 ID 的序列名称设置为给定值,或者(如果值为 nilfalse)设置为给定块返回的值。这对于 Oracle 是必需的,并且对于任何依赖序列进行主键生成的数据库都很有用。

如果在使用 Oracle 时未显式设置序列名称,则默认使用常用的模式:#{table_name}_seq

如果在使用 PostgreSQL 时未显式设置序列名称,它将为您自动查找主键对应的序列。

class Project < ActiveRecord::Base
  self.sequence_name = "projectseq"   # default would have been "project_seq"
end
# File activerecord/lib/active_record/model_schema.rb, line 414
def sequence_name=(value)
  @sequence_name          = value.to_s
  @explicit_sequence_name = true
end

table_exists?()

指示此类对应的表是否存在

# File activerecord/lib/active_record/model_schema.rb, line 432
def table_exists?
  schema_cache.data_source_exists?(table_name)
end

table_name()

根据继承层次结构中直接继承自 ActiveRecord::Base 的类名,推断表名(强制小写)。因此,如果继承层次结构为:Reply < Message < ActiveRecord::Base,则即使在 Reply 上调用,也会使用 Message 来推断表名。用于推断的规则由 Active Support 中的 Inflector 类处理,该类了解几乎所有常见的英语变位。您可以在 config/initializers/inflections.rb 中添加新的变位。

嵌套类的表名将以父类表名的单数形式作为前缀。包含的模块不予考虑。

示例

class Invoice < ActiveRecord::Base
end

file                  class               table_name
invoice.rb            Invoice             invoices

class Invoice < ActiveRecord::Base
  class Lineitem < ActiveRecord::Base
  end
end

file                  class               table_name
invoice.rb            Invoice::Lineitem   invoice_lineitems

module Invoice
  class Lineitem < ActiveRecord::Base
  end
end

file                  class               table_name
invoice/lineitem.rb   Invoice::Lineitem   lineitems

此外,类级别的 table_name_prefix 会被添加在前面,table_name_suffix 会被添加到后面。因此,如果您的前缀是“myapp_”,则 Invoice 类的表名推断将变为“myapp_invoices”。Invoice::Lineitem 将变为“myapp_invoice_lineitems”。

Active Model Naming 的 model_name 是用于推断表名的基本名称。如果定义了自定义 Active Model Name,它也将用于表名。

class PostRecord < ActiveRecord::Base
  class << self
    def model_name
      ActiveModel::Name.new(self, nil, "Post")
    end
  end
end

PostRecord.table_name
# => "posts"

您也可以显式设置自己的表名

class Mouse < ActiveRecord::Base
  self.table_name = "mice"
end
# File activerecord/lib/active_record/model_schema.rb, line 263
def table_name
  reset_table_name unless defined?(@table_name)
  @table_name
end

table_name=(value)

显式设置表名。示例

class Project < ActiveRecord::Base
  self.table_name = "project"
end
# File activerecord/lib/active_record/model_schema.rb, line 273
def table_name=(value)
  value = value && value.to_s

  if defined?(@table_name)
    return if value == @table_name
    reset_column_information if connected?
  end

  @table_name        = value
  @arel_table        = nil
  @sequence_name     = nil unless @explicit_sequence_name
  @predicate_builder = nil
end

实例保护方法

initialize_load_schema_monitor()

# File activerecord/lib/active_record/model_schema.rb, line 565
def initialize_load_schema_monitor
  @load_schema_monitor = Monitor.new
end

reload_schema_from_cache(recursive = true)

# File activerecord/lib/active_record/model_schema.rb, line 569
def reload_schema_from_cache(recursive = true)
  @_returning_columns_for_insert = nil
  @arel_table = nil
  @column_names = nil
  @symbol_column_to_string_name_hash = nil
  @content_columns = nil
  @column_defaults = nil
  @attributes_builder = nil
  @columns = nil
  @columns_hash = nil
  @schema_loaded = false
  @attribute_names = nil
  @yaml_encoder = nil
  if recursive
    subclasses.each do |descendant|
      descendant.send(:reload_schema_from_cache)
    end
  end
end