- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (0)
- 2.0.3 (0)
- 2.1.0 (38)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (1)
- 3.0.9 (-2)
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
Active Record Associations
This is the root class of all association proxies:
AssociationProxy BelongsToAssociation HasOneAssociation BelongsToPolymorphicAssociation AssociationCollection HasAndBelongsToManyAssociation HasManyAssociation HasManyThroughAssociation HasOneThroughAssociation
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.find(: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 has most of the basic instance methods removed, and delegates unknown methods to @target via method_missing. As a corner case, it even removes the class method and that’s why you get
blog.posts.class # => Array
though the object behind blog.posts is not an Array, but an ActiveRecord::Associations::HasManyAssociation.
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.