跳至内容 跳至搜索
方法
B
C
D
I
Q
U

实例公共方法

build(attributes = nil, &block)

构建一个对象(或多个对象),并返回构建的对象或构建的对象列表。

attributes 参数可以是 HashArray 的哈希。这些哈希描述要构建的对象的属性。

示例

# Build a single new object
User.build(first_name: 'Jamie')

# Build an Array of new objects
User.build([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }])

# Build a single object and pass it into a block to set other attributes.
User.build(first_name: 'Jamie') do |u|
  u.is_admin = false
end

# Building an Array of new objects using a block, where the block is executed for each object:
User.build([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }]) do |u|
  u.is_admin = false
end
# File activerecord/lib/active_record/persistence.rb, line 82
def build(attributes = nil, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| build(attr, &block) }
  else
    new(attributes, &block)
  end
end

create(attributes = nil, &block)

创建一个对象(或多个对象),如果验证通过,则将其保存到数据库。无论对象是否成功保存到数据库,都会返回结果对象。

attributes 参数可以是 HashArray 的哈希。这些哈希描述要创建的对象的属性。

示例

# Create a single new object
User.create(first_name: 'Jamie')

# Create an Array of new objects
User.create([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }])

# Create a single object and pass it into a block to set other attributes.
User.create(first_name: 'Jamie') do |u|
  u.is_admin = false
end

# Creating an Array of new objects using a block, where the block is executed for each object:
User.create([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }]) do |u|
  u.is_admin = false
end
# File activerecord/lib/active_record/persistence.rb, line 33
def create(attributes = nil, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| create(attr, &block) }
  else
    object = new(attributes, &block)
    object.save
    object
  end
end

create!(attributes = nil, &block)

创建一个对象(或多个对象),如果验证通过,则将其保存到数据库。如果验证失败,则会引发 RecordInvalid 错误,这与 Base#create 不同。

attributes 参数可以是 HashArray 的哈希。这些描述要在对象上创建的属性,或在给定 Array 哈希时创建的多个对象。

# File activerecord/lib/active_record/persistence.rb, line 50
def create!(attributes = nil, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| create!(attr, &block) }
  else
    object = new(attributes, &block)
    object.save!
    object
  end
end

delete(id_or_array)

使用 SQL DELETE 语句删除与 id 参数匹配的主键的行,并返回已删除的行数。不会实例化 Active Record 对象,因此不会执行对象的回调,包括任何 :dependent 关联选项。

可以通过传递 idArray 一次删除多行。

注意:虽然通常比替代方法 destroy 快得多,但跳过回调可能会绕过应用程序中的业务逻辑,该逻辑可确保引用完整性或执行其他基本任务。

示例

# Delete a single row
Todo.delete(1)

# Delete multiple rows
Todo.delete([2,3,4])
# File activerecord/lib/active_record/persistence.rb, line 565
def delete(id_or_array)
  delete_by(primary_key => id_or_array)
end

destroy(id)

销毁具有给定 id 的对象(或多个对象)。首先实例化对象,因此在删除对象之前会触发所有回调和过滤器。此方法不如 delete 有效,但允许运行清理方法和其他操作。

这实质上是找到具有给定 id 的对象(或多个对象),从属性中创建一个新对象,然后在该对象上调用 destroy。

参数

  • id - 这应该是要销毁的 id 或 id 数组。

示例

# Destroy a single object
Todo.destroy(1)

# Destroy multiple objects
todos = [1,2,3]
Todo.destroy(todos)
# File activerecord/lib/active_record/persistence.rb, line 533
def destroy(id)
  multiple_ids = if composite_primary_key?
    id.first.is_a?(Array)
  else
    id.is_a?(Array)
  end

  if multiple_ids
    find(id).each(&:destroy)
  else
    find(id).destroy
  end
end

insert(attributes, returning: nil, unique_by: nil, record_timestamps: nil)

在单个 SQL INSERT 语句中向数据库中插入单个记录。它不会实例化任何模型,也不会触发 Active Record 回调或验证。尽管传递的值会通过 Active Record 的类型转换和序列化。

有关文档,请参见 insert_all

# File activerecord/lib/active_record/persistence.rb, line 96
def insert(attributes, returning: nil, unique_by: nil, record_timestamps: nil)
  insert_all([ attributes ], returning: returning, unique_by: unique_by, record_timestamps: record_timestamps)
end

insert!(attributes, returning: nil, record_timestamps: nil)

在单个 SQL INSERT 语句中向数据库中插入单个记录。它不会实例化任何模型,也不会触发 Active Record 回调或验证。尽管传递的值会通过 Active Record 的类型转换和序列化。

有关更多信息,请参见 insert_all!

# File activerecord/lib/active_record/persistence.rb, line 185
def insert!(attributes, returning: nil, record_timestamps: nil)
  insert_all!([ attributes ], returning: returning, record_timestamps: record_timestamps)
end

insert_all(attributes, returning: nil, unique_by: nil, record_timestamps: nil)

在单个 SQL INSERT 语句中将多个记录插入数据库。它不会实例化任何模型,也不会触发 Active Record 回调或验证。尽管传递的值会经过 Active Record 的类型转换和序列化。

attributes 参数是哈希的 数组。每个 哈希 确定单行的属性,并且必须具有相同的键。

表中的每个唯一索引都会将行视为唯一。将跳过所有重复行。使用 :unique_by 覆盖(见下文)。

返回一个 ActiveRecord::Result,其内容基于 :returning(见下文)。

选项

:returning

(仅限 PostgreSQL 和 SQLite3)一个属性数组,用于返回所有成功插入记录,默认情况下是主键。传递 returning: %w[ id name ] 以同时获取 id 和 name,或传递 returning: false 以完全省略底层的 RETURNING SQL 子句。

如果你需要对返回值有更多控制,还可以传递一个 SQL 字符串(例如,returning: Arel.sql("id, name as new_name"))。

:unique_by

(仅限 PostgreSQL 和 SQLite)默认情况下,表中的每个唯一索引都会将行视为唯一。将跳过所有重复行。

要仅根据一个唯一索引跳过行,请传递 :unique_by

考虑一个 Book 模型,其中没有重复的 ISBN 有意义,但如果任何行具有现有 id 或不通过另一个唯一索引唯一,则会引发 ActiveRecord::RecordNotUnique

唯一索引可以通过列或名称识别

unique_by: :isbn
unique_by: %i[ author_id name ]
unique_by: :index_books_on_isbn
:record_timestamps

默认情况下,时间戳列的自动设置由模型的 record_timestamps 配置控制,与典型行为相匹配。

要覆盖此设置并强制以某种方式自动设置时间戳列,请传递 :record_timestamps

record_timestamps: true  # Always set timestamps automatically
record_timestamps: false # Never set timestamps automatically

由于它依赖于数据库中的索引信息,因此建议将 :unique_by 与 Active Record 的 schema_cache 配合使用。

示例

# Insert records and skip inserting any duplicates.
# Here "Eloquent Ruby" is skipped because its id is not unique.

Book.insert_all([
  { id: 1, title: "Rework", author: "David" },
  { id: 1, title: "Eloquent Ruby", author: "Russ" }
])

# insert_all works on chained scopes, and you can use create_with
# to set default attributes for all inserted records.

author.books.create_with(created_at: Time.now).insert_all([
  { id: 1, title: "Rework" },
  { id: 2, title: "Eloquent Ruby" }
])
# File activerecord/lib/active_record/persistence.rb, line 175
def insert_all(attributes, returning: nil, unique_by: nil, record_timestamps: nil)
  InsertAll.new(self, attributes, on_duplicate: :skip, returning: returning, unique_by: unique_by, record_timestamps: record_timestamps).execute
end

insert_all!(attributes, returning: nil, record_timestamps: nil)

在单个 SQL INSERT 语句中将多个记录插入数据库。它不会实例化任何模型,也不会触发 Active Record 回调或验证。尽管传递的值会经过 Active Record 的类型转换和序列化。

attributes 参数是哈希的 数组。每个 哈希 确定单行的属性,并且必须具有相同的键。

如果任何行违反表上的唯一索引,则引发 ActiveRecord::RecordNotUnique。在这种情况下,不会插入任何行。

要跳过重复行,请参阅 insert_all。要替换它们,请参阅 upsert_all

返回一个 ActiveRecord::Result,其内容基于 :returning(见下文)。

选项

:returning

(仅限 PostgreSQL 和 SQLite3)一个属性数组,用于返回所有成功插入记录,默认情况下是主键。传递 returning: %w[ id name ] 以同时获取 id 和 name,或传递 returning: false 以完全省略底层的 RETURNING SQL 子句。

如果你需要对返回值有更多控制,还可以传递一个 SQL 字符串(例如,returning: Arel.sql("id, name as new_name"))。

:record_timestamps

默认情况下,时间戳列的自动设置由模型的 record_timestamps 配置控制,与典型行为相匹配。

要覆盖此设置并强制以某种方式自动设置时间戳列,请传递 :record_timestamps

record_timestamps: true  # Always set timestamps automatically
record_timestamps: false # Never set timestamps automatically

示例

# Insert multiple records
Book.insert_all!([
  { title: "Rework", author: "David" },
  { title: "Eloquent Ruby", author: "Russ" }
])

# Raises ActiveRecord::RecordNotUnique because "Eloquent Ruby"
# does not have a unique id.
Book.insert_all!([
  { id: 1, title: "Rework", author: "David" },
  { id: 1, title: "Eloquent Ruby", author: "Russ" }
])
# File activerecord/lib/active_record/persistence.rb, line 242
def insert_all!(attributes, returning: nil, record_timestamps: nil)
  InsertAll.new(self, attributes, on_duplicate: :raise, returning: returning, record_timestamps: record_timestamps).execute
end

instantiate(attributes, column_types = {}, &block)

给定一个属性哈希,instantiate 返回适当类的实例。仅接受字符串形式的键。

例如,Post.all 可能通过将记录的子类存储在 type 属性中来返回评论、消息和电子邮件。通过调用 instantiate 而不是 new,查找器方法确保为每条记录获取适当类的实例。

请参阅 ActiveRecord::Inheritance#discriminate_class_for_record,以了解如何实现此“单表”继承映射。

# File activerecord/lib/active_record/persistence.rb, line 376
def instantiate(attributes, column_types = {}, &block)
  klass = discriminate_class_for_record(attributes)
  instantiate_instance_of(klass, attributes, column_types, &block)
end

query_constraints(*columns_list)

接受要在 SELECT / UPDATE / DELETE 查询的 WHERE 子句中使用的属性名称列表,以及用于“#first` 和 `#last` 查找器方法的 ORDER BY 子句中。

class Developer < ActiveRecord::Base
  query_constraints :company_id, :id
end

developer = Developer.first
# SELECT "developers".* FROM "developers" ORDER BY "developers"."company_id" ASC, "developers"."id" ASC LIMIT 1
developer.inspect # => #<Developer id: 1, company_id: 1, ...>

developer.update!(name: "Nikita")
# UPDATE "developers" SET "name" = 'Nikita' WHERE "developers"."company_id" = 1 AND "developers"."id" = 1

It is possible to update attribute used in the query_by clause:
developer.update!(company_id: 2)
# UPDATE "developers" SET "company_id" = 2 WHERE "developers"."company_id" = 1 AND "developers"."id" = 1

developer.name = "Bob"
developer.save!
# UPDATE "developers" SET "name" = 'Bob' WHERE "developers"."company_id" = 1 AND "developers"."id" = 1

developer.destroy!
# DELETE FROM "developers" WHERE "developers"."company_id" = 1 AND "developers"."id" = 1

developer.delete
# DELETE FROM "developers" WHERE "developers"."company_id" = 1 AND "developers"."id" = 1

developer.reload
# SELECT "developers".* FROM "developers" WHERE "developers"."company_id" = 1 AND "developers"."id" = 1 LIMIT 1
# File activerecord/lib/active_record/persistence.rb, line 488
def query_constraints(*columns_list)
  raise ArgumentError, "You must specify at least one column to be used in querying" if columns_list.empty?

  @query_constraints_list = columns_list.map(&:to_s)
  @has_query_constraints = @query_constraints_list
end

update(id = :all, attributes)

更新一个对象(或多个对象),如果验证通过,则将其保存到数据库。无论对象是否已成功保存到数据库,都将返回结果对象。

参数

  • id - 这应该是要更新的 id 或 id 数组。可选参数,默认为关系中的所有记录。

  • attributes - 这应该是属性哈希或哈希数组。

示例

# Updates one record
Person.update(15, user_name: "Samuel", group: "expert")

# Updates multiple records
people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
Person.update(people.keys, people.values)

# Updates multiple records from the result of a relation
people = Person.where(group: "expert")
people.update(group: "masters")

注意:更新大量记录将为每条记录运行 UPDATE 查询,这可能会导致性能问题。当不需要为每条记录更新运行回调时,最好使用 update_all 在单个查询中更新所有记录。

# File activerecord/lib/active_record/persistence.rb, line 408
def update(id = :all, attributes)
  if id.is_a?(Array)
    if id.any?(ActiveRecord::Base)
      raise ArgumentError,
        "You are passing an array of ActiveRecord::Base instances to `update`. " \
        "Please pass the ids of the objects by calling `pluck(:id)` or `map(&:id)`."
    end
    id.map { |one_id| find(one_id) }.each_with_index { |object, idx|
      object.update(attributes[idx])
    }
  elsif id == :all
    all.each { |record| record.update(attributes) }
  else
    if ActiveRecord::Base === id
      raise ArgumentError,
        "You are passing an instance of ActiveRecord::Base to `update`. " \
        "Please pass the id of the object by calling `.id`."
    end
    object = find(id)
    object.update(attributes)
    object
  end
end

update!(id = :all, attributes)

更新对象(或多个对象),就像 update 一样,但调用 update! 而不是 update,因此如果记录无效且保存失败,则会引发异常。

# File activerecord/lib/active_record/persistence.rb, line 434
def update!(id = :all, attributes)
  if id.is_a?(Array)
    if id.any?(ActiveRecord::Base)
      raise ArgumentError,
        "You are passing an array of ActiveRecord::Base instances to `update!`. " \
        "Please pass the ids of the objects by calling `pluck(:id)` or `map(&:id)`."
    end
    id.map { |one_id| find(one_id) }.each_with_index { |object, idx|
      object.update!(attributes[idx])
    }
  elsif id == :all
    all.each { |record| record.update!(attributes) }
  else
    if ActiveRecord::Base === id
      raise ArgumentError,
        "You are passing an instance of ActiveRecord::Base to `update!`. " \
        "Please pass the id of the object by calling `.id`."
    end
    object = find(id)
    object.update!(attributes)
    object
  end
end

upsert(attributes, **kwargs)

在单个 SQL INSERT 语句中更新或插入(upsert)一条记录到数据库中。它不会实例化任何模型,也不会触发 Active Record 回调或验证。尽管传递的值会经过 Active Record 的类型转换和序列化。

有关文档,请参阅 upsert_all

# File activerecord/lib/active_record/persistence.rb, line 252
def upsert(attributes, **kwargs)
  upsert_all([ attributes ], **kwargs)
end

upsert_all(attributes, on_duplicate: :update, update_only: nil, returning: nil, unique_by: nil, record_timestamps: nil)

在单个 SQL INSERT 语句中更新或插入(upsert)多条记录到数据库中。它不会实例化任何模型,也不会触发 Active Record 回调或验证。尽管传递的值会经过 Active Record 的类型转换和序列化。

attributes 参数是哈希的 数组。每个 哈希 确定单行的属性,并且必须具有相同的键。

返回一个 ActiveRecord::Result,其内容基于 :returning(见下文)。

默认情况下,当发生冲突时,upsert_all 将更新所有可以更新的列。这些列是除主键、只读列和可选的 unique_by 涵盖的列之外的所有列。

选项

:returning

(仅限 PostgreSQL 和 SQLite3)一个属性数组,用于返回所有成功插入记录,默认情况下是主键。传递 returning: %w[ id name ] 以同时获取 id 和 name,或传递 returning: false 以完全省略底层的 RETURNING SQL 子句。

如果你需要对返回值有更多控制,还可以传递一个 SQL 字符串(例如,returning: Arel.sql("id, name as new_name"))。

:unique_by

(仅限 PostgreSQL 和 SQLite)默认情况下,表中的每个唯一索引都会将行视为唯一。将跳过所有重复行。

要仅根据一个唯一索引跳过行,请传递 :unique_by

考虑一个 Book 模型,其中没有重复的 ISBN 有意义,但如果任何行具有现有 id 或不通过另一个唯一索引唯一,则会引发 ActiveRecord::RecordNotUnique

唯一索引可以通过列或名称识别

unique_by: :isbn
unique_by: %i[ author_id name ]
unique_by: :index_books_on_isbn

由于它依赖于数据库中的索引信息,因此建议将 :unique_by 与 Active Record 的 schema_cache 配合使用。

:on_duplicate

配置发生冲突时将使用的 SQL 更新语句。

注意:如果你使用此选项,则必须自己提供要更新的所有列。

示例

Commodity.upsert_all(
  [
    { id: 2, name: "Copper", price: 4.84 },
    { id: 4, name: "Gold", price: 1380.87 },
    { id: 6, name: "Aluminium", price: 0.35 }
  ],
  on_duplicate: Arel.sql("price = GREATEST(commodities.price, EXCLUDED.price)")
)

请参阅相关的 :update_only 选项。这两个选项不能同时使用。

:update_only

提供发生冲突时将更新的列名称列表。如果没有提供,upsert_all 将更新所有可以更新的列。这些列是除主键、只读列和可选的 unique_by 涵盖的列之外的所有列。

示例

Commodity.upsert_all(
  [
    { id: 2, name: "Copper", price: 4.84 },
    { id: 4, name: "Gold", price: 1380.87 },
    { id: 6, name: "Aluminium", price: 0.35 }
  ],
  update_only: [:price] # Only prices will be updated
)

请参阅相关的 :on_duplicate 选项。这两个选项不能同时使用。

:record_timestamps

默认情况下,时间戳列的自动设置由模型的 record_timestamps 配置控制,与典型行为相匹配。

要覆盖此设置并强制以某种方式自动设置时间戳列,请传递 :record_timestamps

record_timestamps: true  # Always set timestamps automatically
record_timestamps: false # Never set timestamps automatically

示例

# Inserts multiple records, performing an upsert when records have duplicate ISBNs.
# Here "Eloquent Ruby" overwrites "Rework" because its ISBN is duplicate.

Book.upsert_all([
  { title: "Rework", author: "David", isbn: "1" },
  { title: "Eloquent Ruby", author: "Russ", isbn: "1" }
], unique_by: :isbn)

Book.find_by(isbn: "1").title # => "Eloquent Ruby"
# File activerecord/lib/active_record/persistence.rb, line 362
def upsert_all(attributes, on_duplicate: :update, update_only: nil, returning: nil, unique_by: nil, record_timestamps: nil)
  InsertAll.new(self, attributes, on_duplicate: on_duplicate, update_only: update_only, returning: returning, unique_by: unique_by, record_timestamps: record_timestamps).execute
end