class ActiveRecord::Associations::CollectionProxy
def select(select = nil, &block)
# #
# #
# => [
person.pets.select(:name) { |pet| pet.name =~ /oo/ }
# ]
# #
# #
# => [
person.pets.select { |pet| pet.name =~ /oo/ }
Array#select.
converting them into an array and iterating through them using
This builds an array of objects from the database for the scope,
*Second:* You can pass a block so it can be used just like Array#select.
# => ActiveModel::MissingAttributeError: missing attribute: person_id
person.pets.select(:name).first.person_id
receive:
to access a field that is not in the initialized record you'll
object with only the fields that you've selected. If you attempt
Be careful because this also means you're initializing a model
# ]
# #
# #
# #
# => [
person.pets.select([:id, :name])
# ]
# #
# #
# #
# => [
person.pets.select(:name)
# ]
# #
# #
# #
# => [
person.pets
end
has_many :pets
class Person < ActiveRecord::Base
*First:* Specify a subset of fields to be selected from the result set.
Works in two ways.
def select(select = nil, &block) @association.select(select, &block) end