跳至内容 跳至搜索

Active Record 集合代理

Active Record 中的集合代理是关联与其目标结果集之间的中间人。

例如,给定

class Blog < ActiveRecord::Base
  has_many :posts
end

blog = Blog.first

blog.posts 返回的集合代理由:has_many 关联构建,并委托给作为目标的帖子集合。

此类通过委托缓存将未知方法委托给关联的关系类。

目标结果集在需要时才加载。例如,

blog.posts.count

直接通过 SQL 计算,并且不会自行触发实际帖子记录的实例化。

方法
#
A
B
C
D
E
F
I
L
M
N
P
R
S
T

实例公共方法

<<(*records)

通过将它们的外部键设置为关联的主键,将一个或多个记录添加到集合中。由于<<会展平其参数列表并插入每个记录,因此pushconcat的行为完全相同。返回self,以便可以将多个追加项链接在一起。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets.size # => 0
person.pets << Pet.new(name: 'Fancy-Fancy')
person.pets << [Pet.new(name: 'Spook'), Pet.new(name: 'Choo-Choo')]
person.pets.size # => 3

person.id # => 1
person.pets
# => [
#      #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#      #<Pet id: 2, name: "Spook", person_id: 1>,
#      #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]
别名:pushappendconcat
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 1036
def <<(*records)
  proxy_association.concat(records) && self
end

==(other)

等同于Array#==。如果两个数组包含相同数量的元素,并且每个元素都等于other数组中的相应元素,则返回true,否则返回false

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets
# => [
#      #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#      #<Pet id: 2, name: "Spook", person_id: 1>
#    ]

other = person.pets.to_ary

person.pets == other
# => true

请注意,未持久化的记录仍然可以视为相等

other = [Pet.new(id: 1), Pet.new(id: 2)]

person.pets == other
# => true
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 967
def ==(other)
  load_target == other
end

any?()

如果集合不为空,则返回true

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets.count # => 0
person.pets.any?  # => false

person.pets << Pet.new(name: 'Snoop')
person.pets.count # => 1
person.pets.any?  # => true

当集合尚未加载时,在没有块的情况下调用它等同于collection.exists?。如果您打算加载集合,最好调用collection.load.any?以避免额外的查询。

您还可以传递一个来定义条件。行为相同,如果基于条件的集合不为空,则返回 true。

person.pets
# => [#<Pet name: "Snoop", group: "dogs">]

person.pets.any? do |pet|
  pet.group == 'cats'
end
# => false

person.pets.any? do |pet|
  pet.group == 'dogs'
end
# => true
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 836
      

append(*records)

别名:<<

build(attributes = {}, &block)

返回集合类型的一个新对象,该对象已使用attributes实例化并链接到此对象,但尚未保存。您可以传递一个属性哈希数组,这将返回一个包含新对象的新数组。

class Person
  has_many :pets
end

person.pets.build
# => #<Pet id: nil, name: nil, person_id: 1>

person.pets.build(name: 'Fancy-Fancy')
# => #<Pet id: nil, name: "Fancy-Fancy", person_id: 1>

person.pets.build([{name: 'Spook'}, {name: 'Choo-Choo'}, {name: 'Brain'}])
# => [
#      #<Pet id: nil, name: "Spook", person_id: 1>,
#      #<Pet id: nil, name: "Choo-Choo", person_id: 1>,
#      #<Pet id: nil, name: "Brain", person_id: 1>
#    ]

person.pets.size  # => 5 # size of the collection
person.pets.count # => 0 # count from database
别名:new
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 318
def build(attributes = {}, &block)
  @association.build(attributes, &block)
end

calculate(operation, column_name)

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 724
def calculate(operation, column_name)
  null_scope? ? scope.calculate(operation, column_name) : super
end

clear()

等同于 delete_all。不同之处在于返回 self,而不是包含已删除对象的数组,以便可以链接方法。有关详细信息,请参阅 delete_all。请注意,由于 delete_all 通过直接在数据库中运行 SQL 查询来删除记录,因此不会更改对象的 updated_at 列。

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 1053
def clear
  delete_all
  self
end

concat(*records)

别名:<<

count(column_name = nil, &block)

计数所有记录。

class Person < ActiveRecord::Base
  has_many :pets
end

# This will perform the count using SQL.
person.pets.count # => 3
person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

传递一个块将在 SQL 中选择所有人的宠物,然后使用 Ruby 执行计数。

person.pets.count { |pet| pet.name.include?('-') } # => 2
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 733
      

create(attributes = {}, &block)

返回已使用属性实例化、链接到此对象且已保存(如果通过验证)的新集合类型对象。

class Person
  has_many :pets
end

person.pets.create(name: 'Fancy-Fancy')
# => #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>

person.pets.create([{name: 'Spook'}, {name: 'Choo-Choo'}])
# => [
#      #<Pet id: 2, name: "Spook", person_id: 1>,
#      #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.size  # => 3
person.pets.count # => 3

person.pets.find(1, 2, 3)
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 349
def create(attributes = {}, &block)
  @association.create(attributes, &block)
end

create!(attributes = {}, &block)

create 类似,但如果记录无效,则引发异常。

class Person
  has_many :pets
end

class Pet
  validates :name, presence: true
end

person.pets.create!(name: nil)
# => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 365
def create!(attributes = {}, &block)
  @association.create!(attributes, &block)
end

delete(*records)

根据 :dependent 选项指定的策略,从集合中删除提供的 records。如果没有提供 :dependent 选项,则它将遵循默认策略。返回一个包含已删除记录的数组。

对于 has_many :through 关联,默认删除策略为 :delete_all

对于 has_many 关联,默认删除策略为 :nullify。这将外键设置为 NULL

class Person < ActiveRecord::Base
  has_many :pets # dependent: :nullify option by default
end

person.pets.size # => 3
person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.delete(Pet.find(1))
# => [#<Pet id: 1, name: "Fancy-Fancy", person_id: 1>]

person.pets.size # => 2
person.pets
# => [
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

Pet.find(1)
# => #<Pet id: 1, name: "Fancy-Fancy", person_id: nil>

如果将其设置为 :destroy,则通过调用其 destroy 方法来删除所有 records。有关更多信息,请参见 destroy

class Person < ActiveRecord::Base
  has_many :pets, dependent: :destroy
end

person.pets.size # => 3
person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.delete(Pet.find(1), Pet.find(3))
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.size # => 1
person.pets
# => [#<Pet id: 2, name: "Spook", person_id: 1>]

Pet.find(1, 3)
# => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 3)

如果将其设置为 :delete_all,则所有 records 都将被删除,调用其 destroy 方法。

class Person < ActiveRecord::Base
  has_many :pets, dependent: :delete_all
end

person.pets.size # => 3
person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.delete(Pet.find(1))
# => [#<Pet id: 1, name: "Fancy-Fancy", person_id: 1>]

person.pets.size # => 2
person.pets
# => [
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

Pet.find(1)
# => ActiveRecord::RecordNotFound: Couldn't find Pet with 'id'=1

您可以传递 IntegerString 值,它会找到响应 id 的记录并在其上执行删除。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets.size # => 3
person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.delete("1")
# => [#<Pet id: 1, name: "Fancy-Fancy", person_id: 1>]

person.pets.delete(2, 3)
# => [
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 620
def delete(*records)
  @association.delete(*records).tap { reset_scope }
end

delete_all(dependent = nil)

根据 :dependent 选项指定的策略,从集合中删除所有记录。如果没有提供 :dependent 选项,则它将遵循默认策略。

对于 has_many :through 关联,默认删除策略为 :delete_all

对于 has_many 关联,默认删除策略为 :nullify。这将外键设置为 NULL

class Person < ActiveRecord::Base
  has_many :pets # dependent: :nullify option by default
end

person.pets.size # => 3
person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.delete_all
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.size # => 0
person.pets      # => []

Pet.find(1, 2, 3)
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: nil>,
#       #<Pet id: 2, name: "Spook", person_id: nil>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: nil>
#    ]

如果 :dependent 选项设置为 :destroy,则 has_manyhas_many :through 依赖项都默认为 :delete_all 策略。不会实例化记录,也不会触发回调。

class Person < ActiveRecord::Base
  has_many :pets, dependent: :destroy
end

person.pets.size # => 3
person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.delete_all

Pet.find(1, 2, 3)
# => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 2, 3)

如果将其设置为 :delete_all,则所有对象都将被删除,调用其 destroy 方法。

class Person < ActiveRecord::Base
  has_many :pets, dependent: :delete_all
end

person.pets.size # => 3
person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.delete_all

Pet.find(1, 2, 3)
# => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 2, 3)
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 474
def delete_all(dependent = nil)
  @association.delete_all(dependent).tap { reset_scope }
end

destroy(*records)

销毁提供的 records 并将其从集合中删除。此方法将始终从数据库中删除记录,而忽略 :dependent 选项。返回一个包含已删除记录的数组。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets.size # => 3
person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.destroy(Pet.find(1))
# => [#<Pet id: 1, name: "Fancy-Fancy", person_id: 1>]

person.pets.size # => 2
person.pets
# => [
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.destroy(Pet.find(2), Pet.find(3))
# => [
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.size  # => 0
person.pets       # => []

Pet.find(1, 2, 3) # => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 2, 3)

您可以传递 IntegerString 值,它会找到响应 id 的记录,然后从数据库中删除它们。

person.pets.size # => 3
person.pets
# => [
#       #<Pet id: 4, name: "Benny", person_id: 1>,
#       #<Pet id: 5, name: "Brain", person_id: 1>,
#       #<Pet id: 6, name: "Boss",  person_id: 1>
#    ]

person.pets.destroy("4")
# => #<Pet id: 4, name: "Benny", person_id: 1>

person.pets.size # => 2
person.pets
# => [
#       #<Pet id: 5, name: "Brain", person_id: 1>,
#       #<Pet id: 6, name: "Boss",  person_id: 1>
#    ]

person.pets.destroy(5, 6)
# => [
#       #<Pet id: 5, name: "Brain", person_id: 1>,
#       #<Pet id: 6, name: "Boss",  person_id: 1>
#    ]

person.pets.size  # => 0
person.pets       # => []

Pet.find(4, 5, 6) # => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (4, 5, 6)
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 692
def destroy(*records)
  @association.destroy(*records).tap { reset_scope }
end

destroy_all()

直接从数据库中删除集合的记录,忽略 :dependent 选项。实例化记录并调用 before_removeafter_removebefore_destroyafter_destroy 回调。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets.size # => 3
person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.destroy_all

person.pets.size # => 0
person.pets      # => []

Pet.find(1) # => Couldn't find Pet with id=1
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 501
def destroy_all
  @association.destroy_all.tap { reset_scope }
end

distinct(value = true)

指定记录是否应该唯一。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets.select(:name)
# => [
#      #<Pet name: "Fancy-Fancy">,
#      #<Pet name: "Fancy-Fancy">
#    ]

person.pets.select(:name).distinct
# => [#<Pet name: "Fancy-Fancy">]

person.pets.select(:name).distinct.distinct(false)
# => [
#      #<Pet name: "Fancy-Fancy">,
#      #<Pet name: "Fancy-Fancy">
#    ]
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 697
      

empty?()

如果集合为空,则返回 true。如果集合已加载,则等同于 collection.size.zero?。如果集合尚未加载,则等同于 !collection.exists?。如果集合尚未加载,并且无论如何你都要获取记录,那么最好检查 collection.load.empty?

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets.count  # => 1
person.pets.empty? # => false

person.pets.delete_all

person.pets.count  # => 0
person.pets.empty? # => true
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 831
def empty?
  @association.empty?
end

fifth()

first 相同,但只返回第五条记录。

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 201
      

find(*args)

在集合中查找响应 id 的对象。使用与 ActiveRecord::FinderMethods.find 相同的规则。如果找不到对象,则返回 ActiveRecord::RecordNotFound 错误。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.find(1) # => #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>
person.pets.find(4) # => ActiveRecord::RecordNotFound: Couldn't find Pet with 'id'=4

person.pets.find(2) { |pet| pet.name.downcase! }
# => #<Pet id: 2, name: "fancy-fancy", person_id: 1>

person.pets.find(2, 3)
# => [
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 138
def find(*args)
  return super if block_given?
  @association.find(*args)
end

first(limit = nil)

从集合中返回第一条记录或前 n 条记录。如果集合为空,第一种形式返回 nil,第二种形式返回一个空数组。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.first # => #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>

person.pets.first(2)
# => [
#      #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#      #<Pet id: 2, name: "Spook", person_id: 1>
#    ]

another_person_without.pets          # => []
another_person_without.pets.first    # => nil
another_person_without.pets.first(3) # => []
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 144
      

forty_two()

first 相同,但只返回第 42 条记录。也称为访问“reddit”。

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 209
      

fourth()

first 相同,但只返回第四条记录。

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 193
      

include?(record)

如果给定的 record 在集合中,则返回 true

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets # => [#<Pet id: 20, name: "Snoop">]

person.pets.include?(Pet.find(20)) # => true
person.pets.include?(Pet.find(21)) # => false
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 927
def include?(record)
  !!@association.include?(record)
end

last(limit = nil)

从集合中返回最后一条记录或最后 n 条记录。如果集合为空,第一种形式返回 nil,第二种形式返回一个空数组。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.last # => #<Pet id: 3, name: "Choo-Choo", person_id: 1>

person.pets.last(2)
# => [
#      #<Pet id: 2, name: "Spook", person_id: 1>,
#      #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

another_person_without.pets         # => []
another_person_without.pets.last    # => nil
another_person_without.pets.last(3) # => []
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 259
def last(limit = nil)
  load_target if find_from_target?
  super
end

length()

返回集合的大小,在目标上调用 size。如果集合已加载,则 lengthsize 是等效的。如果没有,并且无论如何都需要记录,此方法将少进行一次查询。否则,size 更有效率。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets.length # => 3
# executes something like SELECT "pets".* FROM "pets" WHERE "pets"."person_id" = 1

# Because the collection is loaded, you can
# call the collection with no additional queries:
person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 787
      

load_target()

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 44
def load_target
  @association.load_target
end

loaded()

别名:loaded?

loaded?()

如果关联已加载,则返回 true,否则返回 false

person.pets.loaded? # => false
person.pets.records
person.pets.loaded? # => true
也别名为:loaded
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 53
def loaded?
  @association.loaded?
end

many?()

如果集合包含多条记录,则返回 true。相当于 collection.size > 1

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets.count # => 1
person.pets.many? # => false

person.pets << Pet.new(name: 'Snoopy')
person.pets.count # => 2
person.pets.many? # => true

您还可以传递一个 block 来定义条件。行为相同,如果基于条件的集合包含多条记录,则返回 true。

person.pets
# => [
#      #<Pet name: "Gorby", group: "cats">,
#      #<Pet name: "Puff", group: "cats">,
#      #<Pet name: "Snoop", group: "dogs">
#    ]

person.pets.many? do |pet|
  pet.group == 'dogs'
end
# => false

person.pets.many? do |pet|
  pet.group == 'cats'
end
# => true
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 877
      

new(attributes = {}, &block)

别名:build

pluck(*column_names)

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 728
def pluck(*column_names)
  null_scope? ? scope.pluck(*column_names) : super
end

push(*records)

别名:<<

reload()

从数据库重新加载集合。返回 self

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets # fetches pets from the database
# => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]

person.pets # uses the pets cache
# => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]

person.pets.reload # fetches pets from the database
# => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 1072
def reload
  proxy_association.reload(true)
  reset_scope
end

replace(other_array)

使用 other_array 替换此集合。这将执行差异并仅删除/添加已更改的记录。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets
# => [#<Pet id: 1, name: "Gorby", group: "cats", person_id: 1>]

other_pets = [Pet.new(name: 'Puff', group: 'celebrities')]

person.pets.replace(other_pets)

person.pets
# => [#<Pet id: 2, name: "Puff", group: "celebrities", person_id: 1>]

如果提供的数组具有不正确的关联类型,则会引发 ActiveRecord::AssociationTypeMismatch 错误

person.pets.replace(["doo", "ggie", "gaga"])
# => ActiveRecord::AssociationTypeMismatch: Pet expected, got String
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 391
def replace(other_array)
  @association.replace(other_array)
end

reset()

卸载关联。返回 self

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets # fetches pets from the database
# => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]

person.pets # uses the pets cache
# => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]

person.pets.reset # clears the pets cache

person.pets  # fetches pets from the database
# => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 1093
def reset
  proxy_association.reset
  proxy_association.reset_scope
  reset_scope
end

scope()

返回此关联中记录的 Relation 对象

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 936
def scope
  @scope ||= @association.scope
end

second()

first 相同,但仅返回第二个记录。

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 177
      

second_to_last()

last 相同,但仅返回倒数第二个记录。

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 226
      

select(*fields, &block)

以两种方式工作。

第一:指定要从结果集中选择的字段子集。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.select(:name)
# => [
#      #<Pet id: nil, name: "Fancy-Fancy">,
#      #<Pet id: nil, name: "Spook">,
#      #<Pet id: nil, name: "Choo-Choo">
#    ]

person.pets.select(:id, :name)
# => [
#      #<Pet id: 1, name: "Fancy-Fancy">,
#      #<Pet id: 2, name: "Spook">,
#      #<Pet id: 3, name: "Choo-Choo">
#    ]

请小心,因为这也意味着您正在使用仅包含已选择的字段初始化模型对象。如果您尝试访问未初始化记录中除 id 之外的字段,您将收到

person.pets.select(:name).first.person_id
# => ActiveModel::MissingAttributeError: missing attribute 'person_id' for Pet

第二:您可以传递一个块,以便像 Array#select 一样使用它。这会为范围构建一个数据库对象数组,将它们转换为数组并使用 Array#select 遍历它们。

person.pets.select { |pet| /oo/.match?(pet.name) }
# => [
#      #<Pet id: 2, name: "Spook", person_id: 1>,
#      #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 59
      

size()

返回集合的大小。如果尚未加载集合,它将执行 SELECT COUNT(*) 查询。否则,它将调用 collection.size

如果集合已加载,则 sizelength 是等效的。如果没有,并且您无论如何都需要记录,则 length 将减少一次查询。否则,size 更有效率。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets.size # => 3
# executes something like SELECT COUNT(*) FROM "pets" WHERE "pets"."person_id" = 1

person.pets # This will execute a SELECT * FROM query
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.size # => 3
# Because the collection is already loaded, this will behave like
# collection.size and no SQL count query is executed.
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 782
def size
  @association.size
end

take(limit = nil)

使用与 ActiveRecord::FinderMethods.take 相同的规则,从集合中提供一条记录(如果提供参数,则提供 N 条记录)。

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets
# => [
#       #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#       #<Pet id: 2, name: "Spook", person_id: 1>,
#       #<Pet id: 3, name: "Choo-Choo", person_id: 1>
#    ]

person.pets.take # => #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>

person.pets.take(2)
# => [
#      #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
#      #<Pet id: 2, name: "Spook", person_id: 1>
#    ]

another_person_without.pets         # => []
another_person_without.pets.take    # => nil
another_person_without.pets.take(2) # => []
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 289
def take(limit = nil)
  load_target if find_from_target?
  super
end

target()

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 40
def target
  @association.target
end

third()

first 相同,但仅返回第三条记录。

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 185
      

third_to_last()

last 相同,但仅返回第三条最后记录。

# File activerecord/lib/active_record/associations/collection_proxy.rb, line 218