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

实例公共方法

build(attributes = nil, &block)

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

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

示例

# 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 的 Hashes。这些 Hashes 描述了要创建的对象上的属性。

示例

# 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 的 Hashes。这些描述了要在对象上创建的属性,或者在给出 Array 的 Hashes 时创建多个对象。

# 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

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

给定一个属性哈希,instantiate 会返回一个适当类的最新实例。只接受字符串作为键。

例如,Post.all 可能会通过在 type 属性中存储记录的子类来返回 Comments、Messages 和 Emails。通过调用 instantiate 而不是 new,查找方法可以确保为每个记录获取相应类的最新实例。

请参阅 ActiveRecord::Inheritance#discriminate_class_for_record 以了解此“单表”继承映射的实现方式。

# File activerecord/lib/active_record/persistence.rb, line 100
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 an attribute used in the query_constraints 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 212
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 132
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 158
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