Class
ActiveRecord::Associations::CollectionProxy
<
Relation

Association proxies in Active Record are middlemen between the object that
holds the association, known as the @owner, and the actual
associated object, known as the @target. The kind of
association any proxy is about is available in @reflection.
That's an instance of the class
ActiveRecord::Reflection::AssociationReflection.

For example, given

class Blog < ActiveRecord::Base
has_many :posts
end
blog = Blog.first

the association proxy in blog.posts has the object in
blog as @owner, the collection of its posts as
@target, and the @reflection object represents a
:has_many macro.

This class delegates unknown methods to @target via
method_missing.

The @target object is not loaded until needed. For example,

blog.posts.count

is computed directly through SQL and does not trigger by itself the
instantiation of the actual post records.

Returns a new object of the collection type that has been instantiated with
attributes and linked to this object, but have not yet been
saved. You can pass an array of attributes hashes, this will return an
array with the new objects.

Equivalent to delete_all. The difference is that returns
self, instead of an array with the deleted objects, so methods
can be chained. See delete_all for more information. Note that
because delete_all removes records by directly running an SQL
query into the database, the updated_at column of the object
is not changed.

Add one or more records to the collection by setting their foreign keys to
the association's primary key. Since << flattens its argument
list and inserts each record, push and concat behave identically.
Returns self so method calls may be chained.

Deletes the records supplied from the collection according to
the strategy specified by the :dependent option. If no
:dependent option is given, then it will follow the default
strategy. Returns an array with the deleted records.

For has_many :through associations, the default deletion
strategy is :delete_all.

For has_many associations, the default deletion strategy is
:nullify. This sets the foreign keys to NULL.

Deletes the records of the collection directly from the database ignoring
the :dependent option. Records are instantiated and it invokes
before_remove, after_remove ,
before_destroy and after_destroy callbacks.

Returns true if the collection is empty. If the collection has
been loaded it is equivalent to collection.size.zero?. If the
collection has not been loaded, it is equivalent to
collection.exists?. If the collection has not already been
loaded and you are going to fetch the records anyway it is better to check
collection.length.zero?.

Returns the size of the collection calling size on the target.
If the collection has been already loaded, length and
size are equivalent. If not and you are going to need the
records anyway this method will take one less query. Otherwise
size is more efficient.

We don't want this object to be put on the scoping stack, because that
could create an infinite loop where we call an @association method, which
gets the current scope, which is this object, which delegates to
@association, and so on.

Be careful because this also means you're initializing a model object
with only the fields that you've selected. If you attempt to access a
field except id that is not in the initialized record
you'll receive:

Second: You can pass a block so it can be used just like
Array#select. This builds an array of objects from the database for the
scope, converting them into an array and iterating through them using
Array#select.