класс ActiveRecord::Associations::CollectionProxy
Провайдеры ассоциаций в Active Record являются посредниками между объектом, который содержит ассоциацию, известным как @owner, и фактическим связанным объектом, известным как @target. Тип ассоциации, о которой идет речь в любом провайдере, доступен в @reflection. Это экземпляр класса ActiveRecord::Reflection::AssociationReflection.
Например, если
class Blog < ActiveRecord::Base has_many :posts end blog = Blog.first
провайдер ассоциации в blog.posts содержит объект в blog в качестве @owner, коллекцию его постов в качестве @target, и объект @reflection представляет макрос :has_many.
Этот класс делегирует неизвестные методы объекту @target через method_missing.
Объект @target загружается только при необходимости. Например,
blog.posts.count
вычисляется напрямую через SQL и сам по себе не вызывает создание фактических записей о постах.
Публичные методы экземпляра
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 969 def <<(*records) proxy_association.concat(records) && self end
Добавляет одну или несколько records в коллекцию, устанавливая их внешние ключи в первичный ключ ассоциации. Возвращает 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> # ]
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 907 def ==(other) load_target == other end
Эквивалентно Array#==. Возвращает true, если два массива содержат одинаковое количество элементов и если каждый элемент равен соответствующему элементу в массиве other, иначе возвращает 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 # => false
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 805 def any?(&block) @association.any?(&block) end
Возвращает 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 # => 0 person.pets.any? # => true
Вы также можете передать block для определения критериев. Поведение такое же, возвращается 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 861 def arel scope.arel end
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 258
def build(attributes = {}, &block)
@association.build(attributes, &block)
end Возвращает новый объект типа коллекции, который был создан с помощью 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
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 982 def clear delete_all self end
Эквивалентно delete_all. Разница в том, что возвращает self, а не массив с удаленными объектами, поэтому методы могут быть объединены. Смотрите delete_all для получения дополнительной информации.
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 333 def concat(*records) @association.concat(*records) end
Добавить одну или несколько записей в коллекцию, установив их внешние ключи в первичный ключ ассоциации. Так как << выравнивает список своих аргументов и вставляет каждую запись, push и concat ведут себя идентично. Возвращает self, поэтому вызовы методов могут быть объединены.
class Person < ActiveRecord::Base has_many :pets end person.pets.size # => 0 person.pets.concat(Pet.new(name: 'Fancy-Fancy')) person.pets.concat(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> # ] person.pets.concat([Pet.new(name: 'Brain'), Pet.new(name: 'Benny')]) person.pets.size # => 5
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 696
def count(column_name = nil, options = {})
# TODO: Remove options argument as soon we remove support to
# activerecord-deprecated_finders.
@association.count(column_name, options)
end Подсчет всех записей с использованием SQL.
class Person < ActiveRecord::Base has_many :pets end 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> # ]
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 289
def create(attributes = {}, &block)
@association.create(attributes, &block)
end Возвращает новый объект типа коллекции, который был создан с атрибутами, связан с этим объектом и уже сохранен (если он проходит проверки).
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 305
def create!(attributes = {}, &block)
@association.create!(attributes, &block)
end Как 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 588 def delete(*records) @association.delete(*records) end
Удаляет предоставленные records из коллекции в соответствии со стратегией, указанной параметром :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(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, все records удаляются путем вызова их метода destroy. Смотрите 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 IDs (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
Вы можете передать значения Integer или String, он найдет записи, отвечающие id, и выполнит для них delete.
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 442 def delete_all(dependent = nil) @association.delete_all(dependent) end
Удаляет все записи из коллекции в соответствии со стратегией, указанной параметром :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> # ]
Как has_many, так и зависимости +has_many :through+ по умолчанию используют стратегию :delete_all, если параметр :dependent установлен в :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_all Pet.find(1, 2, 3) # => ActiveRecord::RecordNotFound
Если он установлен в :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
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 660 def destroy(*records) @association.destroy(*records) end
Уничтожает предоставленные 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 IDs (1, 2, 3)
Вы можете передать значения Integer или String, он найдет записи, отвечающие 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 IDs (4, 5, 6)
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 469 def destroy_all @association.destroy_all end
Удаляет записи коллекции непосредственно из базы данных, игнорируя параметр :dependent. Записи создаются, и вызываются обратные вызовы before_remove, after_remove , before_destroy и after_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 678 def distinct @association.distinct end
Указывает, должны ли записи быть уникальными или нет.
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">]
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 772 def empty? @association.empty? end
Возвращает true, если коллекция пустая. Если коллекция загружена, это эквивалентно collection.size.zero?. Если коллекция не загружена, это эквивалентно collection.exists?. Если коллекция ещё не загружена, а вам все равно нужно получить записи, лучше проверить collection.length.zero?.
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 190 def fifth(*args) @association.fifth(*args) end
Аналогично first, но возвращает только пятую запись.
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 140 def find(*args, &block) @association.find(*args, &block) end
Ищет объект в коллекции, удовлетворяющий id. Использует те же правила, что и ActiveRecord::Base.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 170 def first(*args) @association.first(*args) end
Возвращает первую запись или первые 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 196 def forty_two(*args) @association.forty_two(*args) end
Аналогично first, но возвращает только сорок вторую запись. Также известно как доступ к «reddit».
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 185 def fourth(*args) @association.fourth(*args) end
Аналогично first, но возвращает только четвёртую запись.
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 857 def include?(record) !!@association.include?(record) end
Возвращает true, если заданная record присутствует в коллекции.
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 226 def last(*args) @association.last(*args) end
Возвращает последнюю запись или последние 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 750 def length @association.length end
Возвращает размер коллекции, вызывая size на целевом объекте. Если коллекция уже загружена, length и size эквивалентны. Если нет, и вам все равно понадобятся записи, этот метод потребует на одну запрос меньше. В противном случае 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 44 def load_target @association.load_target end
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 53 def loaded? @association.loaded? end
Возвращает true, если ассоциация загружена, в противном случае false.
person.pets.loaded? # => false person.pets person.pets.loaded? # => true
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 843 def many?(&block) @association.many?(&block) end
Возвращает 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 975 def prepend(*args) raise NoMethodError, "prepend on association is not defined. Please use << or append" end
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 865 def proxy_association @association end
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 1005 def reload proxy_association.reload self end
Перезагружает коллекцию из базы данных. Возвращает self. Эквивалентно collection(true).
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>] person.pets(true) # 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 359 def replace(other_array) @association.replace(other_array) end
Заменяет эту коллекцию на 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 1026 def reset proxy_association.reset proxy_association.reset_scope self end
Разгружает ассоциацию. Возвращает 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 878 def scope @association.scope end
Возвращает объект Relation для записей в этой ассоциации
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 873
def scoping
@association.scope.scoping { yield }
end Мы не хотим, чтобы этот объект был помещён в стек вычислений, так как это может привести к бесконечной рекурсии, где мы вызываем метод @association, который получает текущий scope, который является этим объектом, который делегирует вызов @association, и так далее.
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 175 def second(*args) @association.second(*args) end
Аналогично first, но возвращает только вторую запись.
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 110 def select(*fields, &block) @association.select(*fields, &block) end
Работает в двух режимах.
Во-первых: Укажите подмножество полей для выбора из набора результатов.
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
Во-вторых: Вы можете передать блок, чтобы его можно было использовать так же, как Array#select. Это создает массив объектов из базы данных для области видимости, преобразует их в массив и итерируется по ним с помощью Array#select.
person.pets.select { |pet| pet.name =~ /oo/ }
# => [
# #<Pet id: 2, name: "Spook", person_id: 1>,
# #<Pet id: 3, name: "Choo-Choo", person_id: 1>
# ]
person.pets.select(:name) { |pet| pet.name =~ /oo/ }
# => [
# #<Pet id: 2, name: "Spook">,
# #<Pet id: 3, name: "Choo-Choo">
# ]
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 726 def size @association.size end
Возвращает размер коллекции. Если коллекция не загружена, выполняется запрос SELECT COUNT(*). В противном случае вызывается collection.size.
Если коллекция уже загружена, то size и length эквивалентны. Если нет, и вам все равно нужны записи, 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 230 def take(n = nil) @association.take(n) end
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 40 def target @association.target end
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 180 def third(*args) @association.third(*args) end
То же самое, что и first, но возвращает только третью запись.
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 944 def to_ary load_target.dup end
Возвращает новый массив объектов из коллекции. Если коллекция не загружена, извлекает записи из базы данных.
class Person < ActiveRecord::Base has_many :pets end 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> # ] other_pets = person.pets.to_ary # => [ # #<Pet id: 4, name: "Benny", person_id: 1>, # #<Pet id: 5, name: "Brain", person_id: 1>, # #<Pet id: 6, name: "Boss", person_id: 1> # ] other_pets.replace([Pet.new(name: 'BooGoo')]) other_pets # => [#<Pet id: nil, name: "BooGoo", person_id: 1>] person.pets # This is not affected by replace # => [ # #<Pet id: 4, name: "Benny", person_id: 1>, # #<Pet id: 5, name: "Brain", person_id: 1>, # #<Pet id: 6, name: "Boss", person_id: 1> # ]
© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.