--- !ruby/object:RI::MethodDescription 
aliases: []

block_params: 
comment: 
- !ruby/struct:SM::Flow::P 
  body: "Adds the following methods for retrieval and query of collections of associated objects: <tt>collection</tt> is replaced with the symbol passed as the first argument, so <tt>has_many :clients</tt> would add among others <tt>clients.empty?</tt>."
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>collection(force_reload = false)</tt> - Returns an array of all the associated objects. An empty array is returned if none are found.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>collection&lt;&lt;(object, ...)</tt> - Adds one or more objects to the collection by setting their foreign keys to the collection's primary key.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>collection.delete(object, ...)</tt> - Removes one or more objects from the collection by setting their foreign keys to <tt>NULL</tt>. This will also destroy the objects if they're declared as <tt>belongs_to</tt> and dependent on this model.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>collection=objects</tt> - Replaces the collections content by deleting and adding objects as appropriate.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>collection_singular_ids</tt> - Returns an array of the associated objects' ids
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>collection_singular_ids=ids</tt> - Replace the collection with the objects identified by the primary keys in <tt>ids</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>collection.clear</tt> - Removes every object from the collection. This destroys the associated objects if they are associated with <tt>:dependent =&gt; :destroy</tt>, deletes them directly from the database if <tt>:dependent =&gt; :delete_all</tt>, otherwise sets their foreign keys to <tt>NULL</tt>.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>collection.empty?</tt> - Returns <tt>true</tt> if there are no associated objects.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>collection.size</tt> - Returns the number of associated objects.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>collection.find</tt> - Finds an associated object according to the same rules as Base.find.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>collection.build(attributes = {}, ...)</tt> - Returns one or more new objects of the collection type that have been instantiated with <tt>attributes</tt> and linked to this object through a foreign key, but have not yet been saved. *Note:* This only works if an associated object already exists, not if it's <tt>nil</tt>!
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>collection.create(attributes = {})</tt> - Returns a new object of the collection type that has been instantiated with <tt>attributes</tt>, linked to this object through a foreign key, and that has already been saved (if it passed the validation). *Note:* This only works if an associated object already exists, not if it's <tt>nil</tt>!
  type: :BULLET
- !ruby/struct:SM::Flow::P 
  body: "Example: A Firm class declares <tt>has_many :clients</tt>, which will add:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: "<tt>Firm#clients</tt> (similar to <tt>Clients.find :all, :conditions =&gt; &quot;firm_id = #{id}&quot;</tt>)"
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Firm#clients&lt;&lt;</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Firm#clients.delete</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Firm#clients=</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Firm#client_ids</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Firm#client_ids=</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Firm#clients.clear</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Firm#clients.empty?</tt> (similar to <tt>firm.clients.size == 0</tt>)
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: "<tt>Firm#clients.size</tt> (similar to <tt>Client.count &quot;firm_id = #{id}&quot;</tt>)"
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: "<tt>Firm#clients.find</tt> (similar to <tt>Client.find(id, :conditions =&gt; &quot;firm_id = #{id}&quot;)</tt>)"
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Firm#clients.build</tt> (similar to <tt>Client.new(&quot;firm_id&quot; =&gt; id)</tt>)
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Firm#clients.create</tt> (similar to <tt>c = Client.new(&quot;firm_id&quot; =&gt; id); c.save; c</tt>)
  type: :BULLET
- !ruby/struct:SM::Flow::P 
  body: The declaration can also include an options hash to specialize the behavior of the association.
- !ruby/struct:SM::Flow::P 
  body: "Options are:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:class_name</tt> - Specify the class name of the association. Use it only if that name can't be inferred from the association name. So <tt>has_many :products</tt> will by default be linked to the Product class, but if the real class name is SpecialProduct, you'll have to specify it with this option.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:conditions</tt> - Specify the conditions that the associated objects must meet in order to be included as a <tt>WHERE</tt> SQL fragment, such as <tt>price &gt; 5 AND name LIKE 'B%'</tt>. Record creations from the association are scoped if a hash is used. <tt>has_many :posts, :conditions =&gt; {:published =&gt; true}</tt> will create published posts with <tt>@blog.posts.create</tt> or <tt>@blog.posts.build</tt>.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:order</tt> - Specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment, such as <tt>last_name, first_name DESC</tt>.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:foreign_key</tt> - Specify the foreign key used for the association. By default this is guessed to be the name of this class in lower-case and &quot;_id&quot; suffixed. So a Person class that makes a <tt>has_many</tt> association will use &quot;person_id&quot; as the default <tt>:foreign_key</tt>.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:dependent</tt> - If set to <tt>:destroy</tt> all the associated objects are destroyed alongside this object by calling their <tt>destroy</tt> method. If set to <tt>:delete_all</tt> all associated objects are deleted <b>without</b> calling their <tt>destroy</tt> method. If set to <tt>:nullify</tt> all associated objects' foreign keys are set to <tt>NULL</tt> <b>without</b> calling their <tt>save</tt> callbacks. *Warning:* This option is ignored when also using the <tt>:through</tt> option.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: "<tt>:finder_sql</tt> - Specify a complete SQL statement to fetch the association. This is a good way to go for complex associations that depend on multiple tables. Note: When this option is used, <tt>find_in_collection</tt> is <em>not</em> added."
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:counter_sql</tt> - Specify a complete SQL statement to fetch the size of the association. If <tt>:finder_sql</tt> is specified but not <tt>:counter_sql</tt>, <tt>:counter_sql</tt> will be generated by replacing <tt>SELECT ... FROM</tt> with <tt>SELECT COUNT(*) FROM</tt>.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:extend</tt> - Specify a named module for extending the proxy. See &quot;Association extensions&quot;.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:include</tt> - Specify second-order associations that should be eager loaded when the collection is loaded.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:group</tt> - An attribute name by which the result should be grouped. Uses the <tt>GROUP BY</tt> SQL-clause.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:limit</tt> - An integer determining the limit on the number of rows that should be returned.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <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.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:select</tt> - By default, this is <tt>*</tt> as in <tt>SELECT * FROM</tt>, but can be changed if you, for example, want to do a join but not include the joined columns. Do not forget to include the primary and foreign keys, otherwise it will rise an error.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:as</tt> - Specifies a polymorphic interface (See <tt>belongs_to</tt>).
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:through</tt> - Specifies a Join Model through which to perform the query. Options for <tt>:class_name</tt> and <tt>:foreign_key</tt> are ignored, as the association uses the source reflection. You can only use a <tt>:through</tt> query through a <tt>belongs_to</tt> or <tt>has_many</tt> association on the join model.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:source</tt> - Specifies the source association name used by <tt>has_many :through</tt> queries. Only use it if the name cannot be inferred from the association. <tt>has_many :subscribers, :through =&gt; :subscriptions</tt> will look for either <tt>:subscribers</tt> or <tt>:subscriber</tt> on Subscription, unless a <tt>:source</tt> is given.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:source_type</tt> - Specifies type of the source association used by <tt>has_many :through</tt> queries where the source association is a polymorphic <tt>belongs_to</tt>.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:uniq</tt> - If true, duplicates will be omitted from the collection. Useful in conjunction with <tt>:through</tt>.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:readonly</tt> - If true, all the associated objects are readonly through the association.
  type: :BULLET
- !ruby/struct:SM::Flow::P 
  body: "Option examples:"
- !ruby/struct:SM::Flow::VERB 
  body: "  has_many :comments, :order =&gt; &quot;posted_on&quot;\n  has_many :comments, :include =&gt; :author\n  has_many :people, :class_name =&gt; &quot;Person&quot;, :conditions =&gt; &quot;deleted = 0&quot;, :order =&gt; &quot;name&quot;\n  has_many :tracks, :order =&gt; &quot;position&quot;, :dependent =&gt; :destroy\n  has_many :comments, :dependent =&gt; :nullify\n  has_many :tags, :as =&gt; :taggable\n  has_many :reports, :readonly =&gt; true\n  has_many :subscribers, :through =&gt; :subscriptions, :source =&gt; :user\n  has_many :subscribers, :class_name =&gt; &quot;Person&quot;, :finder_sql =&gt;\n      'SELECT DISTINCT people.* ' +\n      'FROM people p, post_subscriptions ps ' +\n      'WHERE ps.post_id = #{id} AND ps.person_id = p.id ' +\n      'ORDER BY p.first_name'\n"
full_name: ActiveRecord::Associations::ClassMethods#has_many
is_singleton: false
name: has_many
params: (association_id, options = {}, &extension)
visibility: public
