0
# has_many :people, :extend => FindOrCreateByNameExtension
0
- # ==
== Association Join Models
0
+ # ==
== Association Join Models
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
# @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
+ # === Polymorphic Associations
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
+ # class Asset < ActiveRecord::Base
0
+ # belongs_to :attachable, :polymorphic => true
0
+ # class Post < ActiveRecord::Base
0
+ # has_many :assets, :as => :attachable # The <tt>:as</tt> option specifies the polymorphic interface to use.
0
+ # @asset.attachable = @post
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
# All of the methods are built on a simple caching principle that will keep the result of the last query around unless specifically
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
# 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
# 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
# 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)
Comments
No one has commented yet.