public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
documentation for polymorphic joins

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4084 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
technoweenie (author)
Mon Mar 27 20:01:17 -0800 2006
commit  2597bd6970c394445e7256c1fb9058538422665b
tree    97c7607aab46d33b4e83ebf58d5b488444aaa634
parent  f28d6195347bc2c3c29e49d9684e3f4f5137bed6
...
203
204
205
206
 
207
208
209
...
243
244
245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
247
248
...
465
466
467
 
468
469
470
...
478
479
480
 
481
482
483
484
...
603
604
605
 
606
607
608
609
610
611
 
612
613
614
...
203
204
205
 
206
207
208
209
...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
...
483
484
485
486
487
488
489
...
497
498
499
500
501
502
503
504
...
623
624
625
626
627
628
629
630
631
632
633
634
635
636
0
@@ -203,7 +203,7 @@
0
     # has_many :people, :extend => FindOrCreateByNameExtension
0
     # end
0
     #
0
- # ==== Association Join Models
0
+ # ==== Association Join Models
0
     #
0
     # Has Many associations can be configured with the :through option to use an explicit join model to retrieve the data. This
0
     # operates similarly to a <tt>has_and_belongs_to_many</tt> association. The advantage is that you're able to add validations,
0
@@ -243,6 +243,24 @@
0
     # @firm.clients.collect { |c| c.invoices }.flatten # select all invoices for all clients of the firm
0
     # @firm.invoices # selects all invoices by going through the Client join model.
0
     #
0
+ # === Polymorphic Associations
0
+ #
0
+ # Polymorphic associations on models are not restricted on what types of models they can be associated with. Rather, they
0
+ # specify an interface that a has_many association must adhere to.
0
+ #
0
+ # class Asset < ActiveRecord::Base
0
+ # belongs_to :attachable, :polymorphic => true
0
+ # end
0
+ #
0
+ # class Post < ActiveRecord::Base
0
+ # has_many :assets, :as => :attachable # The <tt>:as</tt> option specifies the polymorphic interface to use.
0
+ # end
0
+ #
0
+ # @asset.attachable = @post
0
+ #
0
+ # This works by using a type column in addition to a foreign key to specify the associated record. In the Asset example, you'd need
0
+ # an attachable_id integer column and an attachable_type string column.
0
+ #
0
     # == Caching
0
     #
0
     # All of the methods are built on a simple caching principle that will keep the result of the last query around unless specifically
0
@@ -465,6 +483,7 @@
0
       # * <tt>:offset</tt>: An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.
0
       # * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not
0
       # include the joined columns.
0
+ # * <tt>:as</tt>: Specifies a polymorphic interface (See #belongs_to).
0
       # * <tt>:through</tt>: Specifies a Join Model to perform the query through. Options for <tt>:class_name</tt> and <tt>:foreign_key</tt>
0
       # are ignored, as the association uses the source reflection. You can only use a <tt>:through</tt> query through a <tt>belongs_to</tt>
0
       # or <tt>has_many</tt> association.
0
@@ -478,6 +497,7 @@
0
       # has_many :people, :class_name => "Person", :conditions => "deleted = 0", :order => "name"
0
       # has_many :tracks, :order => "position", :dependent => :destroy
0
       # has_many :comments, :dependent => :nullify
0
+ # has_many :tags, :as => :taggable
0
       # has_many :subscribers, :through => :subscriptions, :source => :user
0
       # has_many :subscribers, :class_name => "Person", :finder_sql =>
0
       # 'SELECT DISTINCT people.* ' +
0
0
@@ -603,12 +623,14 @@
0
       # is used on the associate class (such as a Post class). You can also specify a custom counter cache column by given that
0
       # name instead of a true/false value to this option (e.g., <tt>:counter_cache => :my_custom_counter</tt>.)
0
       # * <tt>:include</tt> - specify second-order associations that should be eager loaded when this object is loaded.
0
+ # # <tt>:polymorphic</tt> - specify this association is a polymorphic association by passing true.
0
       #
0
       # Option examples:
0
       # belongs_to :firm, :foreign_key => "client_of"
0
       # belongs_to :author, :class_name => "Person", :foreign_key => "author_id"
0
       # belongs_to :valid_coupon, :class_name => "Coupon", :foreign_key => "coupon_id",
0
       # :conditions => 'discounts > #{payments_count}'
0
+ # belongs_to :attachable, :polymorphic => true
0
       def belongs_to(association_id, options = {})
0
         reflection = create_belongs_to_reflection(association_id, options)
0
         

Comments

    No one has commented yet.