- B
- C
- I
- Q
- U
实例公共方法
build(attributes = nil, &block) 链接
构建一个对象(或多个对象)并返回构建的单个对象或一个对象列表。
attributes
参数可以是一个 Hash
或一个 Array
of 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
源代码: 显示 | 在 GitHub 上
# 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
参数可以是一个 Hash
或一个 Array
of 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
源代码: 显示 | 在 GitHub 上
# 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
参数可以是一个 Hash
或一个 Array
of Hashes. 这些 Hashes 描述了要在对象上创建的属性,或者当给定一个 Array
of Hashes 时,将在多个对象上创建的属性。
源代码: 显示 | 在 GitHub 上
# 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
以了解此“单表”继承映射是如何实现的。
源代码: 显示 | 在 GitHub 上
# 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
源代码: 显示 | 在 GitHub 上
# 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 来更新单个查询中的所有记录。
源代码: 显示 | 在 GitHub 上
# 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) 链接
源代码: 显示 | 在 GitHub 上
# 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