Created
May 16, 2011 19:24
-
-
Save lyleunderwood/975130 to your computer and use it in GitHub Desktop.
associations/collection_proxy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 module ActiveRecord | |
2 module Associations | |
3 # Association proxies in Active Record are middlemen between the object that | |
4 # holds the association, known as the <tt>@owner</tt>, and the actual associated | |
5 # object, known as the <tt>@target</tt>. The kind of association any proxy is | |
6 # about is available in <tt>@reflection</tt>. That's an instance of the class | |
7 # ActiveRecord::Reflection::AssociationReflection. | |
8 # | |
9 # For example, given | |
10 # | |
11 # class Blog < ActiveRecord::Base | |
12 # has_many :posts | |
13 # end | |
14 # | |
15 # blog = Blog.find(:first) | |
16 # | |
17 # the association proxy in <tt>blog.posts</tt> has the object in +blog+ as | |
18 # <tt>@owner</tt>, the collection of its posts as <tt>@target</tt>, and | |
19 # the <tt>@reflection</tt> object represents a <tt>:has_many</tt> macro. | |
20 # | |
21 # This class has most of the basic instance methods removed, and delegates | |
22 # unknown methods to <tt>@target</tt> via <tt>method_missing</tt>. As a | |
23 # corner case, it even removes the +class+ method and that's why you get | |
24 # | |
25 # blog.posts.class # => Array | |
26 # | |
27 # though the object behind <tt>blog.posts</tt> is not an Array, but an | |
28 # ActiveRecord::Associations::HasManyAssociation. | |
29 # | |
30 # The <tt>@target</tt> object is not \loaded until needed. For example, | |
31 # | |
32 # blog.posts.count | |
33 # | |
34 # is computed directly through SQL and does not trigger by itself the | |
35 # instantiation of the actual post records. | |
36 class CollectionProxy # :nodoc: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment